By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,617 Members | 1,591 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,617 IT Pros & Developers. It's quick & easy.

Storing Subprocess Results

P: n/a
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.
Sep 2 '08 #1
Share this Question
Share on Google+
3 Replies


P: n/a
On Tue, 2 Sep 2008 07:16:21 -0700 (PDT), topazcode wrote:
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?
Use subprocess.Popen instead of call.

--
Regards,
Wojtek Walczak,
http://tosh.pl/gminick/
Sep 2 '08 #2

P: n/a
On Sep 2, 7:16*am, topazcode <t...@topazcode.comwrote:
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:
>>import commands
commands.getoutput('fortune')
"While money can't buy happiness, it certainly lets you choose your own
\nform of misery."
>>>
Karthik

*Any help
is appreciated. *Thanks.
Sep 2 '08 #3

P: n/a
On Sep 2, 6:31*pm, Karthik Gurusamy <kar1...@gmail.comwrote:
On Sep 2, 7:16*am, topazcode <t...@topazcode.comwrote:
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:>>import commands
>commands.getoutput('fortune')

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

Karthik

*Any help
is appreciated. *Thanks.
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.
Sep 3 '08 #4

This discussion thread is closed

Replies have been disabled for this discussion.