child = pexpect.spawn ('/bin/bash') child.sendline('ls') print(child.readline()) print child.before, child.after
All I get with this code in my output is
ls ls
But when my code is
child = pexpect.spawn('ls') print(child.readline()) print child.before, child.after
Then it works, but only for the first 2 prints. Am I using the wrong send command? I tried send, write, sendline, and couldn't find anymore.
If you wish to read up to the end of the child's output without generating an EOF exception then use the expect(pexpect. EOF) method. TIMEOUT. The expect() and read() methods will also timeout if the child does not generate any output for a given amount of time. If this happens they will raise a TIMEOUT exception.
Pexpect is a Python module for spawning child applications and controlling them automatically. Pexpect can be used for automating interactive applications such as ssh, ftp, passwd, telnet, etc. It can be used to a automate setup scripts for duplicating software package installations on different servers.
Pexpect works like Don Libes' Expect. Pexpect allows your script to spawn a child application and control it as if a human were typing commands. Pexpect can be used for automating interactive applications such as ssh, ftp, passwd, telnet, etc.
In pexpect the before
and after
attributes are populated after an expect
method. The most common thing used in this situation is waiting for the prompt (so you'll know that the previous command finished execution). So, in your case, the code might look something like this:
child = pexpect.spawn ('/bin/bash') child.expect("Your bash prompt here") child.sendline('ls') #If you are using pxssh you can use this #child.prompt() child.expect("Your bash prompt here") print(child.before)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With