Home > Software engineering >  How to get value from ping result?
How to get value from ping result?

Time:12-10

What regular expression must I do in python in re.findall() to get value of percent loss packet? The problem is that the result written to the variable has the form one time:

PING 192.168.1.1 (192.168.1.1) 56(84) bytes of data.

--- 192.168.1.1 ping statistics ---
300 packets transmitted, 300 received, 0% packet loss, time 299466ms
rtt min/avg/max/mdev = 0.976/4.225/26.866/3.043 ms

and the other time:

PING 192.168.1.1 (192.168.1.1) 56(84) bytes of data.

--- 192.168.1.1 ping statistics ---
300 packets transmitted, 299 received, 0,333333% packet loss, time 299452ms
rtt min/avg/max/mdev = 1.231/4.878/19.140/2.807 ms

What the regular expression should look like in code?

wynik_ip_modem = os.popen("ping -c 5 -q 192.168.1.1").read()
wynik_procent = re.findall('???????',wynik_ip_modem)

to read the value of lost packets alone, whether if the ping result is a decimal or an integer?

CodePudding user response:

(\d*[\.|\,]?\d )% The breakdown can be seen here: https://regex101.com/r/amVdfY/3

The reason this works is % is only found in the packet loss part.

  • Related