xiaozai
Posts: 13
Joined: Fri Sep 02, 2016 7:27 am

Copy packet loss value in terminal into python

Tue Nov 01, 2016 6:00 am

i have the following code to ping a computer and copy the packet loss value from the ping result

Code: Select all

 import os 
            hostname = "192.168.0.255"
            packetloss = os.system ("ping -c 5 " + hostname+ " | grep -oP '\d+(?=% packet loss')") 
            print (packetloss, 'is packet loss value')  
The result that i get if 100% packet loss is

Code: Select all

100
           (0, 'is packet loss value')
and if there is no packet loss

Code: Select all

0
          (0, 'is packet loss value')


How can i make the above value the same? it always detect 0 on packet loss value.

User avatar
rpdom
Posts: 17275
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: Copy packet loss value in terminal into python

Tue Nov 01, 2016 6:25 am

os.system just returns the exit code of the command, which will be 0 (success) in this case. You should probably be using subprocess instead of os.system, but you could try os.popen for now

Code: Select all

import os
hostname = "192.168.0.255"
packetloss = os.popen ("ping -c 5 " + hostname+ " | grep -oP '\d+(?=% packet loss)' ").read()
print (packetloss, 'is packet loss value')  
Last edited by rpdom on Tue Nov 01, 2016 6:35 am, edited 1 time in total.

xiaozai
Posts: 13
Joined: Fri Sep 02, 2016 7:27 am

Re: Copy packet loss value in terminal into python

Tue Nov 01, 2016 6:30 am

change to os.popen. give me errors. "grep: write error: Broken pipe". i try to change to subprocess later

User avatar
rpdom
Posts: 17275
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: Copy packet loss value in terminal into python

Tue Nov 01, 2016 6:36 am

xiaozai wrote:change to os.popen. give me errors. "grep: write error: Broken pipe". i try to change to subprocess later
Funny, the code I posted above (now I fixed the indentation from your original), works for me

Code: Select all

$ python testprog
('100\n', 'is packet loss value')

xiaozai
Posts: 13
Joined: Fri Sep 02, 2016 7:27 am

Re: Copy packet loss value in terminal into python

Tue Nov 01, 2016 6:42 am

my bad. there is typing error on my side. thank you for the code. work nicely :)

xiaozai
Posts: 13
Joined: Fri Sep 02, 2016 7:27 am

Re: Copy packet loss value in terminal into python

Thu Nov 03, 2016 6:12 am

just notice something. how can i eliminate "\n" from the packet loss value?

User avatar
rpdom
Posts: 17275
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: Copy packet loss value in terminal into python

Thu Nov 03, 2016 6:33 am

One way is to use rstrip()

Code: Select all

import os
hostname = "192.168.0.255"
packetloss = os.popen ("ping -c 5 " + hostname+ " | grep -oP '\d+(?=% packet loss)' ").read().rstrip()
print (packetloss, 'is packet loss value')
The '\n' is the Linux "newline" character and a simple internet search for "Python remove trailing newline" came up with loads of examples.

xiaozai
Posts: 13
Joined: Fri Sep 02, 2016 7:27 am

Re: Copy packet loss value in terminal into python

Thu Nov 03, 2016 6:40 am

is there gonna be an error if use this command instead?

Code: Select all

print(packetloss.rstrip(), 'loss value')

User avatar
rpdom
Posts: 17275
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: Copy packet loss value in terminal into python

Thu Nov 03, 2016 6:45 am

xiaozai wrote:is there gonna be an error if use this command instead?

Code: Select all

print(packetloss.rstrip(), 'loss value')
Try it and find out? ;-)

You might even want to try

Code: Select all

print packetloss.rstrip() + ' loss value'
Last edited by rpdom on Thu Nov 03, 2016 6:47 am, edited 1 time in total.

xiaozai
Posts: 13
Joined: Fri Sep 02, 2016 7:27 am

Re: Copy packet loss value in terminal into python

Thu Nov 03, 2016 6:46 am

already try it. hoping there are no error if i keep using this code. thanks alot :)

xiaozai
Posts: 13
Joined: Fri Sep 02, 2016 7:27 am

Re: Copy packet loss value in terminal into python

Thu Nov 03, 2016 8:10 am

Im confuse a little bit. i add another code:

Code: Select all

if packetloss <= 10:
                   print(packetloss, 'is packet loss value')
                   print('this is below 10')
elif packetloss >= 11:
                   print(packetloss, 'is packet loss value')
                   print('this is above 11') 
else:
                   print('invalid number')
However every time i run the code, it always detect above 11 even though the loss value is 0. is there any coding error i have done?

User avatar
elParaguayo
Posts: 1943
Joined: Wed May 16, 2012 12:46 pm
Location: London, UK

Re: Copy packet loss value in terminal into python

Thu Nov 03, 2016 8:33 am

My guess is that you are comparing a string value for packetloss with an integer.

Convert your string to an integer first.

Edit: this should illustrate the problem.

Code: Select all

In [1]: packetloss = "0"

In [2]: packetloss <= 10
Out[2]: False

In [3]: packetloss >= 11
Out[3]: True

In [4]: packetloss = 0

In [5]: packetloss <= 10
Out[5]: True

In [6]: packetloss >= 11
Out[6]: False
RPi Information Screen: plugin based system for displaying weather, travel information, football scores etc.

Return to “Python”