【问题标题】:pexpect behaves differently on same inputpexpect 在相同的输入上表现不同
【发布时间】:2017-11-02 10:04:50
【问题描述】:

我使用 pexpect 登录瞻博网络防火墙并发送相同的命令两次,我希望输出(我的意思是 s.before)应该是相同的,但它们不是。为什么 pexpect 在同一命令上表现不同?

下面是python测试文件。

import pxssh
import getpass
try:
    s = pxssh.pxssh()
    hostname = '192.168.215.254'
    username = 'root'
    password = 'engine@1'
    s.login (hostname, username, password, original_prompt='root@developfirewall%', auto_prompt_reset=False)
    s.sendline ('cli')  # run a command
    s.PROMPT = 'root@developfirewall>'
    s.prompt()           # match the prompt
    print s.before       # print everything before the prompt.
    s.sendline ('configure')
    s.PROMPT = 'root@developfirewall#'
    s.prompt()
    print s.before
    print 'start---------------------------------------------'
    s.sendline ('delete interfaces ge-0/0/5 unit 0 family inet address 10.123.0.13/32')
    s.prompt()
    print s.before
    print 'end---------------------------------------------'
    print 'start---------------------------------------------'
    s.sendline ('delete interfaces ge-0/0/5 unit 0 family inet address 10.123.0.13/32')
    s.prompt()
    print s.before
    print 'end---------------------------------------------'
    s.logout()
except pxssh.ExceptionPxssh, e:
    print "pxssh failed on login."
    print str(e)

下面是标准输出,我可以看到相同的删除命令返回不同的响应。

 cli


 configure 
Entering configuration mode
Users currently editing the configuration:
  root terminal p1 (pid 64918) on since 2017-11-02 15:18:07 UTC, idle 01:34:05
      [edit]
  root terminal p0 (pid 64649) on since 2017-11-02 14:38:45 UTC, idle 02:07:38
      [edit]
  root terminal p4 (pid 64737) on since 2017-11-02 14:56:05 UTC, idle 01:55:21
      [edit]
The configuration has been changed but not committed

[edit]

start---------------------------------------------
 delete interfaces ge-0/0/5 unit 0 family inet addres
end---------------------------------------------
start---------------------------------------------
 ...0/5 unit 0 family inet address                       10.123.0.13/32 
delete interfaces ge-0/0/5 unit 0 family inet address 10.123.0.13/32warning: statement not found

[edit]

end---------------------------------------------

下面是对juniper防火墙的手动操作,对两个delete命令的响应是一样的。

[clouder@sky27 ~]$ ssh root@juniperfw
--- JUNOS 12.1X44-D35.5 built 2014-05-19 21:36:43 UTC

root@developfirewall% cli
root@developfirewall> configure 
Entering configuration mode
Users currently editing the configuration:
  root terminal p1 (pid 64918) on since 2017-11-02 15:18:07 UTC, idle 01:37:27
      [edit]
  root terminal p4 (pid 64737) on since 2017-11-02 14:56:05 UTC, idle 01:58:43
      [edit]
The configuration has been changed but not committed

[edit]
root@developfirewall# delete interfaces ge-0/0/5 unit 0 family inet address 10.123.0.13/32 
warning: statement not found

[edit]
root@developfirewall# delete interfaces ge-0/0/5 unit 0 family inet address 10.123.0.13/32    
warning: statement not found

[edit]
root@developfirewall# 

【问题讨论】:

    标签: python expect pexpect


    【解决方案1】:

    可能发生的情况是匹配提示时出现超时,这会导致命令发送过早。 s.prompt() 的默认超时时间是在您使用pxssh.pxssh(timeout=...) 创建对象时设置的,为 30 秒。如果提示调用超时,它将返回 False。

    除此之外,您还可以使用print s.before 进行调试,您可以设置一个文件来登录或登录到stderr:pxssh.pxssh(logfile=sys.stderr)

    【讨论】:

    • 一般来说,“屏幕抓取”有 2 个主要的时间问题(如今已接近皱眉):1)提示需要很长时间才能显示 2)下一个命令发送太快(或更糟糕的是,之前发送过)提示出现。 meuh 涵盖了第一个问题 (pxssh.pxssh(timeout=...)),添加了一些 sleep 语句涵盖了第一个问题。
    猜你喜欢
    • 2019-04-24
    • 1970-01-01
    • 1970-01-01
    • 2010-11-28
    • 1970-01-01
    • 2017-11-08
    • 1970-01-01
    • 1970-01-01
    • 2019-02-11
    相关资源
    最近更新 更多