Python如何从pexpect孩子读取输出?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17632010/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Python how to read output from pexpect child?
提问by user2579116
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.
然后它可以工作,但仅适用于前 2 次打印。我是否使用了错误的发送命令?我试过发送,写,发送线,但再也找不到了。
回答by Reegan Miranda
#!/usr/bin/env python
import pexpect
child = pexpect.spawn("ssh [email protected] -p 2222")
child.logfile = open("/tmp/mylog", "w")
child.expect(".*assword:")
child.send("XXXXXXX\r")
child.expect(".*$ ")
child.sendline("ls\r")
child.expect(".*$ ")
go to open your logfile:- go to terminal
去打开你的日志文件:-去终端
$gedit /tmp/mylog
As Per https://pexpect.readthedocs.io/en/stable/api/pexpect.html#spawn-class
根据https://pexpect.readthedocs.io/en/stable/api/pexpect.html#spawn-class
# In Python 3, we'll use the ``encoding`` argument to decode data
# from the subprocess and handle it as unicode:
child = pexpect.spawn('some_command', encoding='utf-8')
child.logfile = sys.stdout
回答by Catalin Luta
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:
在 pexpectbefore
和after
属性在expect
方法之后填充。在这种情况下最常用的方法是等待提示(这样您就会知道上一个命令已完成执行)。因此,在您的情况下,代码可能如下所示:
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)
回答by Stanislav
I think all you need is:
我认为你需要的是:
p = pexpect.spawn('ls')
p.expect(pexpect.EOF)
print(p.before)
or
或者
p = pexpect.spawn('/bin/ls')
p.expect(pexpect.EOF)
print(p.before)
or
或者
p = pexpect.spawn('/bin/bash -c "ls"')
p.expect(pexpect.EOF)
print(p.before)
or even
甚至
print(pexpect.run('ls'))
回答by mvensky
Try the following:
请尝试以下操作:
import pexpect
child = pexpect.spawn('ls')
print child.read() # not readline
The read()
will give you the entire output of the ls.
该read()
会给你LS的整个输出。
回答by djhaskin987
import sys
import pexpect
child = pexpect.spawn('ls')
child.logfile = sys.stdout
child.expect(pexpect.EOF)
See the manual entry on the subject.
请参阅有关该主题的手册条目。
回答by yongxu
copy from class spawn(SpawnBase) docstring, maybe example-2 is what you want.
从类 spawn(SpawnBase) 文档字符串复制,也许 example-2 是您想要的。
Example log input and output to a file::
示例日志输入和输出到文件::
child = pexpect.spawn('some_command')
fout = open('mylog.txt','wb')
child.logfile = fout
Example log to stdout::
示例日志到标准输出::
# In Python 2:
child = pexpect.spawn('some_command')
child.logfile = sys.stdout
# In Python 3, we'll use the ``encoding`` argument to decode data
# from the subprocess and handle it as unicode:
child = pexpect.spawn('some_command', encoding='utf-8')
child.logfile = sys.stdout