472,141 Members | 1,303 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,141 software developers and data experts.

Storing Subprocess Results

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
3 2049
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
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
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.

Similar topics

2 posts views Thread by Chermside, Michael | last post: by
12 posts views Thread by Eric_Dexter | last post: by
1 post views Thread by The Cruisemaniac | last post: by
7 posts views Thread by skunkwerk | last post: by
1 post views Thread by King | last post: by
reply views Thread by dudeja.rajat | last post: by
2 posts views Thread by dudeja.rajat | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.