是否可以让pexpect输出与之匹配的文本? [英] Is it possbile to let pexpect output the texts it matches?
问题描述
我对Expect脚本很熟悉,因此当我第一次使用pexpect时会感到有些奇怪.以这个简单的脚本为例,
I am familiar with expect script so I feel a bit odd when I first use pexpect. Take this simple script as an example,
#!/usr/bin/expect
set timeout 10
spawn npm login
expect "Username:"
send "qiulang2000\r"
expect "Password:"
send "xxxxx\r"
expect "Email:"
send "qiulang@gmail.com\r"
expect "Logged in as"
interact
运行时,我将得到以下输出.感觉很自然,因为那是我运行这些命令的方式
When run it I will get the following output. It feels natural because that is how I run those commands
spawn npm login
Username: qiulang2000
Password:
Email: (this IS public) qiulang@gmail.com
Logged in as qiulang2000 on https://registry.npmjs.com/.
但是当我使用pexpect时,无论我如何添加print(child.after)
或print(child.before)
,我都无法获得期望的输出,例如当我运行以下命令时,
But when I use pexpect, no matter how I add print(child.after)
or print(child.before)
I just can't get output like expect, e.g. when I run following command,
#! /usr/bin/env python3
import pexpect
child = pexpect.spawn('npm login')
child.timeout = 10
child.expect('Username:')
print(child.after.decode("utf-8"))
child.sendline('qiulang2000')
child.expect('Password:')
child.sendline('xxxx')
child.expect('Email:')
child.sendline('qiulang@gmail.com')
child.expect('Logged in as')
print(child.before.decode("utf-8"))
child.interact()
我得到了这些输出,感觉不自然,因为那不是我在运行这些命令时看到的.
I got these output, it feels unnatural because that is not what I see when I run those commands.
Username:
(this IS public) qiulang@gmail.com
qiulang2000 on https://registry.npmjs.com/.
那么有可能实现预期的脚本输出吗?
So is it it possbile to achieve the expect script output?
-更新---
有了我从@pynexj得到的评论,我终于使它生效了,请在下面检查我的答案.
With the comment I got from @pynexj I finally make it work, check my answer below.
推荐答案
有了我的评论,我终于使它起作用了
With the comment I got I finally made it work
#! /usr/bin/env python3
import pexpect
import sys
print('npm login',timeout = 10)
child = pexpect.spawn('npm login')
child.logfile_read = sys.stdout.buffer // use sys.stdout.buffer for python3
child.expect('Username:')
child.sendline('qiulang2000')
child.expect('Password:')
child.sendline('xxxx')
child.expect('Email:')
child.sendline('qiulang@gmail.com')
child.expect('Logged in as')
如果我需要调用child.interact(),那么在它之前调用child.logfile_read = None
是很重要的,否则sys.stdout会回显我键入的所有内容.
If I need to call child.interact(), then it is important that I call child.logfile_read = None
before it, otherwise sys.stdout will echo everything I type.
此处的答案如何在pexpect中查看输出?说我需要为python3传递编码,但是我发现如果使用encoding='utf-8'
它将导致TypeError: a bytes-like object is required, not 'str'
如果我根本不设置编码,一切都会正常.
The answer here How to see the output in pexpect? said I need to pass an encoding for python3, but I found that if I use encoding='utf-8'
it will cause TypeError: a bytes-like object is required, not 'str'
If I don't set encoding at all, everything works fine.
因此,一个简单的ssh登录脚本如下所示:
So a simple ssh login script looks like this
#!/usr/bin/env python3
import pexpect
import sys
child = pexpect.spawn('ssh qiulang@10.0.0.32')
child.logfile_read = sys.stdout.buffer
child.expect('password:')
child.sendline('xxxx')
#child.expect('Last login') don't do that
child.logfile_read = None # important !!!
child.interact()
一个问题仍然没有解决,我在发送密码后添加了最后一个期望调用以匹配ssh登录输出,例如child.expect('最后登录')
One problem remains unresolved, I had added one last expect call to match the ssh login output after sending the password, e.g. child.expect('Last login')
但是,如果我添加了该行,则该行将显示两次.我放弃了尝试,就像一条评论说"pexpect的行为有点违反直觉".
But if I added that, that line would show twice. I have gave up trying, like one comment said "pexpect's behavior is kind of counter intuitive".
Welcome to Ubuntu 16.04 LTS (GNU/Linux 4.4.0-141-generic x86_64)
* Documentation: https://help.ubuntu.com/
33 packages can be updated.
0 updates are security updates.
Last login: Fri Sep 11 11:44:19 2020 from 10.0.0.132
: Fri Sep 11 11:44:19 2020 from 10.0.0.132
这篇关于是否可以让pexpect输出与之匹配的文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!