Python how to read output from pexpect child?

后端 未结 6 1816
攒了一身酷
攒了一身酷 2020-12-29 22:51
child = pexpect.spawn (\'/bin/bash\')
child.sendline(\'ls\')
print(child.readline())
print child.before, child.after

All I get with this code in my

6条回答
  •  没有蜡笔的小新
    2020-12-29 22:55

    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)
    

提交回复
热议问题