python - Split check_output return value -


i trying run unix command using python, have got code return value want, not seem let me split value on delimiter have specified

import subprocess subprocess import check_output  def runfping(command):     output = check_output(command.split(" "))     output = str(output).split(" : ")     return output  print runfping("fping -c 1 -q 192.168.1.25") 

the output is:

10.1.30.10 : 29.00 [''] 

it looks fping writing stderr. capture both stderr , stdout output using check_output, use

output = check_output(command.split(" "),stderr=subprocess.stdout) 

see https://docs.python.org/2/library/subprocess.html#subprocess.check_output

in code

#!/usr/bin/env python import subprocess subprocess import check_output  def runfping(command):     output = check_output(command.split(" "),stderr=subprocess.stdout))     output = str(output).split(" : ")     return output  if __name__ == "__main__":     print runfping("fping -c 1 -q 192.168.1.25") 

will result in

192.168.1.25 : 0.04 ['192.168.1.25', '0.04\n'] 

Comments

Popular posts from this blog

html - Firefox flex bug applied to buttons? -

html - Missing border-right in select on Firefox -

mysql - FireDac error 314 - but DLLs are in program directory -