<s> import getopt <EOL> import os <EOL> import signal <EOL> import sys <EOL> import time <EOL> from statistics import stdev <EOL> import dns . rdatatype <EOL> import dns . resolver <EOL> __VERSION__ = <NUM_LIT:1.0> <EOL> __PROGNAME__ = os . path . basename ( sys . argv [ <NUM_LIT:0> ] ) <EOL> should_stop = False <EOL> resolvers = dns . resolver . get_default_resolver ( ) . nameservers <EOL> def usage ( ) : <EOL> print ( """<STR_LIT>""" % ( __PROGNAME__ , __VERSION__ , __PROGNAME__ ) ) <EOL> exit ( ) <EOL> def signal_handler ( sig , frame ) : <EOL> global should_stop <EOL> if should_stop : <EOL> exit ( <NUM_LIT:0> ) <EOL> should_stop = True <EOL> def maxlen ( names ) : <EOL> sn = sorted ( names , key = len ) <EOL> return len ( sn [ - <NUM_LIT:1> ] ) <EOL> def dnsping ( host , server , dnsrecord , timeout , count ) : <EOL> resolver = dns . resolver . Resolver ( ) <EOL> resolver . nameservers = [ server ] <EOL> resolver . timeout = timeout <EOL> resolver . lifetime = timeout <EOL> resolver . retry_servfail = <NUM_LIT:0> <EOL> response_time = [ ] <EOL> i = <NUM_LIT:0> <EOL> for i in range ( count ) : <EOL> if should_stop : <EOL> break <EOL> try : <EOL> stime = time . time ( ) <EOL> answers = resolver . query ( host , dnsrecord ) <EOL> etime = time . time ( ) <EOL> except ( dns . resolver . NoNameservers , dns . resolver . NoAnswer ) : <EOL> break <EOL> except dns . resolver . Timeout : <EOL> pass <EOL> else : <EOL> elapsed = ( etime - stime ) * <NUM_LIT:1000> <EOL> response_time . append ( elapsed ) <EOL> r_sent = i + <NUM_LIT:1> <EOL> r_received = len ( response_time ) <EOL> r_lost = r_sent - r_received <EOL> r_lost_percent = ( <NUM_LIT:100> * r_lost ) / r_sent <EOL> if response_time : <EOL> r_min = min ( response_time ) <EOL> r_max = max ( response_time ) <EOL> r_avg = sum ( response_time ) / r_received <EOL> r_stddev = stdev ( response_time ) <EOL> else : <EOL> r_min = <NUM_LIT:0> <EOL> r_max = <NUM_LIT:0> <EOL> r_avg = <NUM_LIT:0> <EOL> r_stddev = <NUM_LIT:0> <EOL> return ( server , r_avg , r_min , r_max , r_stddev , r_lost_percent ) <EOL> def main ( ) : <EOL> try : <EOL> signal . signal ( signal . SIGTSTP , signal . SIG_IGN ) <EOL> signal . signal ( signal . SIGINT , signal_handler ) <EOL> except AttributeError : <EOL> pass <EOL> if len ( sys . argv ) == <NUM_LIT:1> : <EOL> usage ( ) <EOL> dnsrecord = '<STR_LIT:A>' <EOL> count = <NUM_LIT:10> <EOL> waittime = <NUM_LIT:5> <EOL> inputfilename = None <EOL> fromfile = False <EOL> hostname = '<STR_LIT>' <EOL> try : <EOL> opts , args = getopt . getopt ( sys . argv [ <NUM_LIT:1> : ] , "<STR_LIT>" , <EOL> [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] ) <EOL> except getopt . GetoptError as err : <EOL> print ( err ) <EOL> usage ( ) <EOL> if args and len ( args ) == <NUM_LIT:1> : <EOL> hostname = args [ <NUM_LIT:0> ] <EOL> else : <EOL> usage ( ) <EOL> for o , a in opts : <EOL> if o in ( "<STR_LIT>" , "<STR_LIT>" ) : <EOL> usage ( ) <EOL> elif o in ( "<STR_LIT:-c>" , "<STR_LIT>" ) : <EOL> count = int ( a ) <EOL> elif o in ( "<STR_LIT>" , "<STR_LIT>" ) : <EOL> inputfilename = a <EOL> fromfile = True <EOL> elif o in ( "<STR_LIT>" , "<STR_LIT>" ) : <EOL> waittime = int ( a ) <EOL> elif o in ( "<STR_LIT>" , "<STR_LIT>" ) : <EOL> dnsrecord = a <EOL> else : <EOL> print ( "<STR_LIT>" % o ) <EOL> usage ( ) <EOL> try : <EOL> if fromfile : <EOL> with open ( inputfilename , '<STR_LIT>' ) as flist : <EOL> f = flist . read ( ) . splitlines ( ) <EOL> else : <EOL> f = resolvers <EOL> if len ( f ) == <NUM_LIT:0> : <EOL> print ( "<STR_LIT>" ) <EOL> f = [ name . strip ( ) for name in f ] <EOL> width = maxlen ( f ) <EOL> blanks = ( width - <NUM_LIT:5> ) * '<STR_LIT:U+0020>' <EOL> print ( '<STR_LIT>' , blanks , '<STR_LIT>' ) <EOL> print ( ( <NUM_LIT> + width ) * '<STR_LIT:->' ) <EOL> for server in f : <EOL> if not server : <EOL> continue <EOL> ( server , r_avg , r_min , r_max , r_stddev , r_lost_percent ) = dnsping ( hostname , server , dnsrecord , waittime , count ) <EOL> server = server . ljust ( width + <NUM_LIT:1> ) <EOL> print ( "<STR_LIT>" % ( <EOL> server , r_avg , r_min , r_max , r_stddev , r_lost_percent ) ) <EOL> except Exception as e : <EOL> print ( '<STR_LIT>' % e ) <EOL> exit ( <NUM_LIT:1> ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> main ( ) </s>
