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
3335minmax_matcher = re .compile (r'(\d+.\d+)/(\d+.\d+)/(\d+.\d+)/(\d+.\d+)' )
3638format_replacements = [('%h' , 'host' ),
3739 ('%s' , 'sent' ),
3840 ('%r' , 'received' ),
41+ ('%d' , 'duplicates' ),
3942 ('%p' , 'packet_loss' ),
4043 ('%m' , 'minping' ),
4144 ('%a' , 'avgping' ),
4649default_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
0 commit comments