pexpect在相同的输入上表现不同

时间:2017-11-02 10:04:50

标签: python expect pexpect

我使用pexpect登录juniper防火墙并发送相同的命令两次,我希望输出(我的意思是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)

下面是stdout输出,我可以看到相同的delete命令返回不同的响应。

 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防火墙上的手动操作,它对两个删除命令的响应相同。

[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# 

1 个答案:

答案 0 :(得分:0)

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

相反,或者使用print s.before进行调试,您可以设置要登录的文件,或者登录到stderr:pxssh.pxssh(logfile=sys.stderr)

相关问题