From c2709fd1bfab44db585e4f50162464f894fb4944 Mon Sep 17 00:00:00 2001 From: Babak Farrokhi Date: Sun, 10 Apr 2016 15:36:29 +0430 Subject: [PATCH] fix layout with variable len addresses (close #4) --- dnseval.py | 36 ++++++++++++++++++++++++------------ 1 file changed, 24 insertions(+), 12 deletions(-) diff --git a/dnseval.py b/dnseval.py index 36a4bff..992c43b 100755 --- a/dnseval.py +++ b/dnseval.py @@ -25,6 +25,8 @@ # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +import dns.rdatatype +import dns.resolver import getopt import os import signal @@ -32,9 +34,6 @@ import sys import time from statistics import stdev -import dns.rdatatype -import dns.resolver - __VERSION__ = 1.0 __PROGNAME__ = os.path.basename(sys.argv[0]) should_stop = False @@ -53,7 +52,7 @@ resolvers = [ def usage(): 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(' -f --file dns server list to use') 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 +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): resolver = dns.resolver.Resolver() resolver.nameservers = [server] @@ -111,8 +118,7 @@ def dnsping(host, server, dnsrecord, timeout, count): r_avg = 0 r_stddev = 0 - print("%-15s %-8.3f %-8.3f %-8.3f %-8.3f %%%d" % ( - server, r_avg, r_min, r_max, r_stddev, r_lost_percent)) + return (server, r_avg, r_min, r_max, r_stddev, r_lost_percent) def main(): @@ -159,18 +165,24 @@ def main(): try: if fromfile: - f = open(inputfilename, 'rt') + with open(inputfilename, 'rt') as flist: + f = flist.read().splitlines() else: f = resolvers - print('server avg(ms) min(ms) max(ms) stddev(ms) lost(%)') - print('--------------------------------------------------------------------------') + width = widest_len(f) + blanks = (width - 5) * ' ' + print('server ', blanks, ' avg(ms) min(ms) max(ms) stddev(ms) lost(%)') + print((60 + width) * '-') for server in f: s = server.strip() if not s: continue - dnsping(hostname, s, dnsrecord, waittime, count) - if fromfile: - f.close() + (server, r_avg, r_min, r_max, r_stddev, r_lost_percent) = dnsping(hostname, s, dnsrecord, waittime, count) + + 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: print('error: %s' % e) exit(1)