I am attempting on Python 2.6.6 to get the routing table of a system (for a interface) into a python list to parse; but I cannot seem to solve why the entire result is stored into one variable.
The loop seems to iterate over one characters at a time, while the behavior I wanted was one line at a time.
what I get is one character; short example below...
1
0
.
2
4
3
what I'd like line to return; so I 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 is the code below...
def getnet(int):
int = 'eth0' ####### for testing only
cmd = ('route -n | grep ' + int)
routes = subprocess.Popen([cmd], shell=True, stdout=subprocess.PIPE)
routes, err = routes.communicate()
for line in routes:
print line
routes
in your case is a bytestring that contains the entire output from the shell command.for character in astring
statement produces one character at a time as the example in your question demonstrates. To get lines as a list of strings instead, calllines = all_output.splitlines()
:Here's a workaround if your Python version has no
check_output()
. If you want to read one line at a time while the process is still running, see Python: read streaming input from subprocess.communicate().You could try to use
os
,ctypes
modules, to get the info instead of grepping the output of an external command.