Reputation: 175
I am struggling to use subprocesses with python. Here is my task:
What I've attempted thus far:
1. I am stuck here using the Popen. I understand that if I use
subprocess.call("put command here")
this works. I wanted to try to use something similar to:
import subprocess
def run_command(command):
p = subprocess.Popen(command, shell=True,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT)
where I use run_command("insert command here")
but this does nothing.
with respect to 2. I think the answer should be similar to here: Running shell command from Python and capturing the output, but as I can't get 1. to work, I haven't tried that yet.
Upvotes: 6
Views: 26469
Reputation: 90496
You can look into Pexpect, a module specifically designed for interacting with shell-based programs.
For example launching a scp command and waiting for password prompt you do:
child = pexpect.spawn('scp foo [email protected]:.')
child.expect ('Password:')
child.sendline (mypassword)
See Pexpect-u for a Python 3 version.
Upvotes: 7
Reputation: 12765
To at least really start the subprocess, you have to tell the Popen-object to really communicate.
def run_command(command):
p = subprocess.Popen(command, shell=True,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT)
return p.communicate()
Upvotes: 9