Storing Subprocess Results

T

topazcode

I am using the subprocess module to run some shell commands on a Linux
system:

import subprocess
output = subprocess.call('''ssh server1 "uptime"''', shell=True)

The above assigns the output variable with a return code, i.e. 0 in
this case. How can I actually capture the data returned from
subprocess.call, rather than just the return code? I'd like to have
the output variable contain the uptime string in this case. Any help
is appreciated. Thanks.
 
K

Karthik Gurusamy

I am using the subprocess module to run some shell commands on a Linux
system:

import subprocess
output = subprocess.call('''ssh server1 "uptime"''', shell=True)

The above assigns the output variable with a return code, i.e. 0 in
this case.  How can I actually capture the data returned from
subprocess.call, rather than just the return code?  I'd like to have
the output variable contain the uptime string in this case.

Probably commands module is a better choice for your problem:"While money can't buy happiness, it certainly lets you choose your own
\nform of misery."
Karthik

 Any help
 
T

topazcode

Probably commands module is a better choice for your problem:>>> import commands

"While money can't buy happiness, it certainly lets you choose your own
\nform of misery."



Karthik

 Any help

Thanks guys. I went ahead and used subprocess.Popen as suggested and
that works fine. Did something like:

import subprocess
subprocess.Popen("uptime", shell=True, stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
stdout_value, stderr_value = subprocess.communicate()

The above worked great. The 'uptime' was actually a fairly long
stretch of commands, and this allows me to check for STDERR and act
accordingly. Thanks again for the help and suggestions.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top