Skip to content

Commit 92beb0c

Browse files
+ add support of duplicates
+ update rst doc * update testdata
1 parent d88a969 commit 92beb0c

6 files changed

Lines changed: 22 additions & 7 deletions

File tree

README.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ a dictionary containing the parsed fields::
3030
... 2 packets transmitted, 2 received, 0% packet loss, time 5072ms
3131
... rtt min/avg/max/mdev = 13.946/17.682/21.418/3.736 ms''')
3232
>>> OrderedDict(sorted(results.items()))
33-
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')])
33+
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')])
3434

3535

3636
Installing

pingparser.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@
2727

2828
# This one works on OS X output which includes the percentage in 0.0% format
2929
# https://regex101.com/r/nmjQzI/2
30-
rslt_matcher = re.compile(r'(\d+) packets transmitted, (\d+) (?:packets )?received, (\d+\.?\d*)% packet loss')
30+
rslt_matcher = re.compile(r'(\d+) packets transmitted, (\d+) (?:packets )?received')
31+
dups_matcher = re.compile(r'\+(\d+) duplicates')
32+
loss_matcher = re.compile(r'(\d+\.?\d*)% packet loss')
3133

3234
# Pull out round-trip min/avg/max/stddev = 49.042/49.042/49.042/0.000 ms
3335
minmax_matcher = re.compile(r'(\d+.\d+)/(\d+.\d+)/(\d+.\d+)/(\d+.\d+)')
@@ -36,6 +38,7 @@
3638
format_replacements = [('%h', 'host'),
3739
('%s', 'sent'),
3840
('%r', 'received'),
41+
('%d', 'duplicates'),
3942
('%p', 'packet_loss'),
4043
('%m', 'minping'),
4144
('%a', 'avgping'),
@@ -46,12 +49,14 @@
4649
default_format = ','.join([fmt for fmt, field in format_replacements])
4750

4851

49-
def _get_match_groups(ping_output, regex):
52+
def _get_match_groups(ping_output, regex, default=None):
5053
"""
5154
Get groups by matching regex in output from ping command.
5255
"""
5356
match = regex.search(ping_output)
5457
if not match:
58+
if default is not None:
59+
return default
5560
raise Exception('Invalid PING output:\n' + ping_output)
5661
return match.groups()
5762

@@ -74,7 +79,9 @@ def parse(ping_output):
7479
in milliseconds
7580
"""
7681
host = _get_match_groups(ping_output, host_matcher)[0]
77-
sent, received, packet_loss = _get_match_groups(ping_output, rslt_matcher)
82+
sent, received = _get_match_groups(ping_output, rslt_matcher)
83+
duplicates, = _get_match_groups(ping_output, dups_matcher, default=('0',))
84+
packet_loss, = _get_match_groups(ping_output, loss_matcher)
7885

7986
try:
8087
minping, avgping, maxping, jitter = _get_match_groups(ping_output,
@@ -85,6 +92,7 @@ def parse(ping_output):
8592
return {'host' : host,
8693
'sent' : sent,
8794
'received' : received,
95+
'duplicates' : duplicates,
8896
'packet_loss' : packet_loss,
8997
'minping' : minping,
9098
'avgping' : avgping,
@@ -129,6 +137,7 @@ def main(argv=sys.argv):
129137
\t%h host name or IP address
130138
\t%s packets sent
131139
\t%r packets received
140+
\t%d duplicates
132141
\t%p packet_loss
133142
\t%m minimum ping in milliseconds
134143
\t%a average ping in milliseconds

test/data/dup.testdata

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
172.17.100.10,61,61,167,0,143.743,167.150,197.045,22.298
2+
PING 172.17.100.10 (172.17.100.10) 56(84) bytes of data.
3+
4+
--- 172.17.100.10 ping statistics ---
5+
61 packets transmitted, 61 received, +167 duplicates, 0% packet loss, time 119997ms
6+
rtt min/avg/max/mdev = 143.743/167.150/197.045/22.298 ms

test/data/original.testdata

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
www.l.google.com,2,2,0,13.946,17.682,21.418,3.736
1+
www.l.google.com,2,2,0,0,13.946,17.682,21.418,3.736
22
PING www.l.google.com (74.125.225.84) 56(84) bytes of data.
33
64 bytes from 74.125.225.84: icmp_req=1 ttl=55 time=13.9 ms
44
64 bytes from 74.125.225.84: icmp_req=2 ttl=55 time=21.4 ms

test/data/yahoo.ping.testdata.debian

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
yahoo.com,1,1,0,0.557,0.557,0.557,0.000
1+
yahoo.com,1,1,0,0,0.557,0.557,0.557,0.000
22
PING yahoo.com (98.138.253.109): 56 data bytes
33
64 bytes from 98.138.253.109: icmp_seq=0 ttl=37 time=0.557 ms
44
--- yahoo.com ping statistics ---

test/data/yahoo.ping.testdata.osx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
yahoo.com,1,1,0.0,27.409,27.409,27.409,0.000
1+
yahoo.com,1,1,0,0.0,27.409,27.409,27.409,0.000
22
PING yahoo.com (98.139.183.24): 56 data bytes
33
64 bytes from 98.139.183.24: icmp_seq=0 ttl=48 time=27.409 ms
44

0 commit comments

Comments
 (0)