Skip to content Skip to sidebar Skip to footer

Storing Value From A Parsed Ping

I'm working on some code that performs a ping operation from python and extracts only the latency by using awk. This is currently what I have: from os import system l = system('pin

Solution 1:

Use subprocess.check_output if you want to store the output in a variable:

from subprocess import check_output
l = check_output("ping -c 1 sitename | awk -F = 'FNR==2 {print substr($4,1,length($4)-3)}'", shell=True) 
print l

Related: Extra zero after executing a python script

Solution 2:

os.system() returns the return code of the called command, not the output to stdout.

For detail on how to properly get the command's output (including pre-Python 2.7), see this: Running shell command from Python and capturing the output

Solution 3:

BTW I would use Ping Package https://pypi.python.org/pypi/ping

It looks promising

Solution 4:

Here is how I store output to a variable.

test=$(ping -c 1 google.com | awk -F"=| "'NR==2 {print $11}')
echo"$test"
34.9

Post a Comment for "Storing Value From A Parsed Ping"