Probably the easiest way is to use pexpect . Example (from overview in the wiki):
import pexpect child = pexpect.spawn('ftp ftp.openbsd.org') child.expect('Name .*: ') child.sendline('anonymous') child.expect('Password:') child.sendline(' noah@example.com ') child.expect('ftp> ') child.sendline('cd pub') child.expect('ftp> ') child.sendline('get ls-lR.gz') child.expect('ftp> ') child.sendline('bye')
If you do not want to use the add-on module, using subprocess.Popen
is the way to go, but it is more complicated. First you create a process.
import subprocess script = subprocess.Popen(['script.sh'], stdin=subprocess.PIPE, stdout=subprocess.PIPE, shell=True)
Here you can use shell=True
or add the shell name to the command arguments. The first is simpler.
Then you need to read script.stdout
until you find your question number. Then you write the response to script.stdin
.
source share