【问题标题】:Pexpect does not get the output correctlyPexpect 无法正确获得输出
【发布时间】:2017-10-09 18:23:41
【问题描述】:

我正在使用 Pexpect 与远程防火墙进行交互。交互中的一切都很好,但是,当我尝试使用 child.before 获取输出时,我没有得到我想要的。这是我的代码:

child = pexpect.spawn('ssh admin@X.X.X.X')
child.sendline(password)
child.expect('>')
child.sendline('set cli pager off')
child.expect('>')
child.send('show system info')
child.expect('>')
child.before

我在 child.before 语句中得到的输出是下一个:

' 设置 cli \r\x1b[Kadmin@name(active)'

在防火墙端,这是我收到的交互(直接从 child.interact() 复制):

admin@ctsmefw1(active)> set cli pager off
admin@ctsmefw1(active)> show system info

hostname: name
ip-address: ip
# ...There are more lines but these are omitted here...

admin@name(active)> 

所以与防火墙的交互是正确的。为什么我不能得到输出?我究竟做错了什么?

感谢您的帮助,问候

迈克

【问题讨论】:

  • 由于您直接使用ssh 进行连接,您是否考虑过使用Paramiko 而不是期望?它会更快,您不必担心终端问题。

标签: python ssh pexpect


【解决方案1】:

您应该将child.send('show system info') 更改为child.sendline('show system info')

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-09
    • 1970-01-01
    • 2020-12-03
    • 1970-01-01
    • 2011-12-27
    相关资源
    最近更新 更多