fix layout with variable len addresses (close #4)

This commit is contained in:
Babak Farrokhi 2016-04-10 15:36:29 +04:30
parent c2748f52a7
commit c2709fd1bf
Signed by: farrokhi
GPG Key ID: 6B267AD85D632E9A

View File

@ -25,6 +25,8 @@
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
import dns.rdatatype
import dns.resolver
import getopt import getopt
import os import os
import signal import signal
@ -32,9 +34,6 @@ import sys
import time import time
from statistics import stdev from statistics import stdev
import dns.rdatatype
import dns.resolver
__VERSION__ = 1.0 __VERSION__ = 1.0
__PROGNAME__ = os.path.basename(sys.argv[0]) __PROGNAME__ = os.path.basename(sys.argv[0])
should_stop = False should_stop = False
@ -53,7 +52,7 @@ resolvers = [
def usage(): def usage():
print('%s version %1.1f\n' % (__PROGNAME__, __VERSION__)) print('%s version %1.1f\n' % (__PROGNAME__, __VERSION__))
print('syntax: %s [-h] [-f server-list] [-c count] [-t type] [-w wait] hostname' % __PROGNAME__ ) print('syntax: %s [-h] [-f server-list] [-c count] [-t type] [-w wait] hostname' % __PROGNAME__)
print(' -h --help show this help') print(' -h --help show this help')
print(' -f --file dns server list to use') print(' -f --file dns server list to use')
print(' -c --count number of requests to send (default: 10)') print(' -c --count number of requests to send (default: 10)')
@ -69,6 +68,14 @@ def signal_handler(sig, frame):
should_stop = True # pressed once, exit gracefully should_stop = True # pressed once, exit gracefully
def widest_len(names):
width = 0
for s in names:
if len(s) > width:
width = len(s)
return width
def dnsping(host, server, dnsrecord, timeout, count): def dnsping(host, server, dnsrecord, timeout, count):
resolver = dns.resolver.Resolver() resolver = dns.resolver.Resolver()
resolver.nameservers = [server] resolver.nameservers = [server]
@ -111,8 +118,7 @@ def dnsping(host, server, dnsrecord, timeout, count):
r_avg = 0 r_avg = 0
r_stddev = 0 r_stddev = 0
print("%-15s %-8.3f %-8.3f %-8.3f %-8.3f %%%d" % ( return (server, r_avg, r_min, r_max, r_stddev, r_lost_percent)
server, r_avg, r_min, r_max, r_stddev, r_lost_percent))
def main(): def main():
@ -159,18 +165,24 @@ def main():
try: try:
if fromfile: if fromfile:
f = open(inputfilename, 'rt') with open(inputfilename, 'rt') as flist:
f = flist.read().splitlines()
else: else:
f = resolvers f = resolvers
print('server avg(ms) min(ms) max(ms) stddev(ms) lost(%)') width = widest_len(f)
print('--------------------------------------------------------------------------') blanks = (width - 5) * ' '
print('server ', blanks, ' avg(ms) min(ms) max(ms) stddev(ms) lost(%)')
print((60 + width) * '-')
for server in f: for server in f:
s = server.strip() s = server.strip()
if not s: if not s:
continue continue
dnsping(hostname, s, dnsrecord, waittime, count) (server, r_avg, r_min, r_max, r_stddev, r_lost_percent) = dnsping(hostname, s, dnsrecord, waittime, count)
if fromfile:
f.close() server = server.ljust(width + 1)
print("%s %-8.3f %-8.3f %-8.3f %-8.3f %%%d" % (
server, r_avg, r_min, r_max, r_stddev, r_lost_percent))
except Exception as e: except Exception as e:
print('error: %s' % e) print('error: %s' % e)
exit(1) exit(1)