Control a process interactively (pexpect) and save data

Piotrek G. piotrek at nonexistent.domain
Wed May 6 16:20:01 CEST 2009


Hi,

I'm trying to use something like pexpect.interact() but I want to save 
all my inputs and save all outputs from the process (/bin/sh Linux).
The main goal is to record all sequence of commands and responses in 
order to automatically generate pexpect script.

My script is like below so far, but I'm unable to print responses to 
stdout and save it to variables/files/whatever.

p = pexpect.spawn('/bin/sh')
print "PID: " + str(p.pid)
p.logfile = sys.stdout
while True:
     if not p.isalive():
         print "Not alive"
         break
     else:
         print "Alive!"
     p.flush()
     bb = sys.stdin.readline()
     p.sendline(bb)
sys.exit(0)

pexpect.interact() doesn't allow to save input and output.

I tried pipes but I've found that "Don't use a pipe to control another 
application..." - http://edgysoftware.com/doc/python-pexpect/doc/

I tried subprocess module but it didn't allow me to control /bin/sh as 
communicate() method do as follows
"Interact with process: Send data to stdin. Read data from stdout and 
stderr, until end-of-file is reached. Wait for process to terminate. The 
optional input argument should be a string to be sent to the child 
process, or None, if no data should be sent to the child."

So it "waits for process to terminate" and I'm unable to interact...

Any suggestions?

Oh, and by the way my script works with /bin/sh but doesn't work with 
/bin/bash. If I enter 'exit' sh exits, bash does not... Why?

Thanks!
-- 
Regards
Piotrek



More information about the Python-list mailing list