ash ash - 1 year ago 129
Python Question

check output from CalledProcessError

I am using subprocess.check_output from pythons subprocess module to execute a ping command. Here is how I am doing it:

output = subprocess.check_output(["ping","-c 2 -W 2","")

It is raising a CalledProcessError and says the output is one of the arguments of the function. Can anyone help me how to read that output. I would like to read the output into a string and parse it. So say for example if the ping returns

100% packet loss

I need to capture that. If there is any other better way..please suggest. Thanks.

Answer Source

In the list of arguments, each entry must be on its own. Using

output = subprocess.check_output(["ping", "-c","2", "-W","2", ""])

should fix your problem.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download