From 92beb0ca7279396020259d26c416b585e36b33d1 Mon Sep 17 00:00:00 2001 From: "Shell.Xu" Date: Tue, 2 Jul 2019 17:25:11 +0800 Subject: [PATCH] + add support of duplicates + update rst doc * update testdata --- README.rst | 2 +- pingparser.py | 15 ++++++++++++--- test/data/dup.testdata | 6 ++++++ test/data/original.testdata | 2 +- test/data/yahoo.ping.testdata.debian | 2 +- test/data/yahoo.ping.testdata.osx | 2 +- 6 files changed, 22 insertions(+), 7 deletions(-) create mode 100644 test/data/dup.testdata diff --git a/README.rst b/README.rst index 6f0132c..d8d3722 100755 --- a/README.rst +++ b/README.rst @@ -30,7 +30,7 @@ a dictionary containing the parsed fields:: ... 2 packets transmitted, 2 received, 0% packet loss, time 5072ms ... rtt min/avg/max/mdev = 13.946/17.682/21.418/3.736 ms''') >>> OrderedDict(sorted(results.items())) - OrderedDict([('avgping', '17.682'), ('host', 'www.l.google.com'), ('jitter', '3.736'), ('maxping', '21.418'), ('minping', '13.946'), ('packet_loss', '0'), ('received', '2'), ('sent', '2')]) + OrderedDict([('avgping', '17.682'), ('duplicates', '0'), ('host', 'www.l.google.com'), ('jitter', '3.736'), ('maxping', '21.418'), ('minping', '13.946'), ('packet_loss', '0'), ('received', '2'), ('sent', '2')]) Installing diff --git a/pingparser.py b/pingparser.py index 1ab9941..2f4889c 100755 --- a/pingparser.py +++ b/pingparser.py @@ -27,7 +27,9 @@ # This one works on OS X output which includes the percentage in 0.0% format # https://regex101.com/r/nmjQzI/2 -rslt_matcher = re.compile(r'(\d+) packets transmitted, (\d+) (?:packets )?received, (\d+\.?\d*)% packet loss') +rslt_matcher = re.compile(r'(\d+) packets transmitted, (\d+) (?:packets )?received') +dups_matcher = re.compile(r'\+(\d+) duplicates') +loss_matcher = re.compile(r'(\d+\.?\d*)% packet loss') # Pull out round-trip min/avg/max/stddev = 49.042/49.042/49.042/0.000 ms minmax_matcher = re.compile(r'(\d+.\d+)/(\d+.\d+)/(\d+.\d+)/(\d+.\d+)') @@ -36,6 +38,7 @@ format_replacements = [('%h', 'host'), ('%s', 'sent'), ('%r', 'received'), + ('%d', 'duplicates'), ('%p', 'packet_loss'), ('%m', 'minping'), ('%a', 'avgping'), @@ -46,12 +49,14 @@ default_format = ','.join([fmt for fmt, field in format_replacements]) -def _get_match_groups(ping_output, regex): +def _get_match_groups(ping_output, regex, default=None): """ Get groups by matching regex in output from ping command. """ match = regex.search(ping_output) if not match: + if default is not None: + return default raise Exception('Invalid PING output:\n' + ping_output) return match.groups() @@ -74,7 +79,9 @@ def parse(ping_output): in milliseconds """ host = _get_match_groups(ping_output, host_matcher)[0] - sent, received, packet_loss = _get_match_groups(ping_output, rslt_matcher) + sent, received = _get_match_groups(ping_output, rslt_matcher) + duplicates, = _get_match_groups(ping_output, dups_matcher, default=('0',)) + packet_loss, = _get_match_groups(ping_output, loss_matcher) try: minping, avgping, maxping, jitter = _get_match_groups(ping_output, @@ -85,6 +92,7 @@ def parse(ping_output): return {'host' : host, 'sent' : sent, 'received' : received, + 'duplicates' : duplicates, 'packet_loss' : packet_loss, 'minping' : minping, 'avgping' : avgping, @@ -129,6 +137,7 @@ def main(argv=sys.argv): \t%h host name or IP address \t%s packets sent \t%r packets received + \t%d duplicates \t%p packet_loss \t%m minimum ping in milliseconds \t%a average ping in milliseconds diff --git a/test/data/dup.testdata b/test/data/dup.testdata new file mode 100644 index 0000000..a2ed757 --- /dev/null +++ b/test/data/dup.testdata @@ -0,0 +1,6 @@ +172.17.100.10,61,61,167,0,143.743,167.150,197.045,22.298 +PING 172.17.100.10 (172.17.100.10) 56(84) bytes of data. + +--- 172.17.100.10 ping statistics --- +61 packets transmitted, 61 received, +167 duplicates, 0% packet loss, time 119997ms +rtt min/avg/max/mdev = 143.743/167.150/197.045/22.298 ms \ No newline at end of file diff --git a/test/data/original.testdata b/test/data/original.testdata index 80880f8..8fa6ee9 100644 --- a/test/data/original.testdata +++ b/test/data/original.testdata @@ -1,4 +1,4 @@ -www.l.google.com,2,2,0,13.946,17.682,21.418,3.736 +www.l.google.com,2,2,0,0,13.946,17.682,21.418,3.736 PING www.l.google.com (74.125.225.84) 56(84) bytes of data. 64 bytes from 74.125.225.84: icmp_req=1 ttl=55 time=13.9 ms 64 bytes from 74.125.225.84: icmp_req=2 ttl=55 time=21.4 ms diff --git a/test/data/yahoo.ping.testdata.debian b/test/data/yahoo.ping.testdata.debian index 82ba9cd..1b99bf8 100644 --- a/test/data/yahoo.ping.testdata.debian +++ b/test/data/yahoo.ping.testdata.debian @@ -1,4 +1,4 @@ -yahoo.com,1,1,0,0.557,0.557,0.557,0.000 +yahoo.com,1,1,0,0,0.557,0.557,0.557,0.000 PING yahoo.com (98.138.253.109): 56 data bytes 64 bytes from 98.138.253.109: icmp_seq=0 ttl=37 time=0.557 ms --- yahoo.com ping statistics --- diff --git a/test/data/yahoo.ping.testdata.osx b/test/data/yahoo.ping.testdata.osx index b5d5b48..ccdddda 100644 --- a/test/data/yahoo.ping.testdata.osx +++ b/test/data/yahoo.ping.testdata.osx @@ -1,4 +1,4 @@ -yahoo.com,1,1,0.0,27.409,27.409,27.409,0.000 +yahoo.com,1,1,0,0.0,27.409,27.409,27.409,0.000 PING yahoo.com (98.139.183.24): 56 data bytes 64 bytes from 98.139.183.24: icmp_seq=0 ttl=48 time=27.409 ms