python formatting return value of subprocess -
i attempting on python 2.6.6 routing table of system (for interface) python list parse; cannot seem solve why entire result stored 1 variable.
the loop seems iterate on 1 characters @ time, while behavior wanted 1 line @ time.
what 1 character; short example below...
1 0 . 2 4 3
what i'd line return; can run other commands against each line..
10.243.186.1 0.0.0.0 255.255.255.255 uh 0 0 0 eth0 10.243.188.1 0.0.0.0 255.255.255.255 uh 0 0 0 eth0 10.243.184.0 10.243.186.1 255.255.255.128 ug 0 0 0 eth0
here code below...
def getnet(int): int = 'eth0' ####### testing cmd = ('route -n | grep ' + int) routes = subprocess.popen([cmd], shell=true, stdout=subprocess.pipe) routes, err = routes.communicate() line in routes: print line
routes
in case bytestring contains entire output shell command. for character in astring
statement produces 1 character @ time example in question demonstrates. lines list of strings instead, call lines = all_output.splitlines()
:
from subprocess import check_output lines = check_output("a | b", shell=true, universal_newlines=true).splitlines()
here's a workaround if python version has no check_output()
. if want read 1 line @ time while process still running, see python: read streaming input subprocess.communicate().
you try use os
, ctypes
modules, info instead of grepping output of external command.
Comments
Post a Comment