Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to get value from ping result?

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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?

>Solution :

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

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading