Python pxssh期望模式不匹配'#'

时间:2018-05-31 16:17:15

标签: python pexpect pxssh

我正在尝试使用pxssh连接到多个设备并从中收集数据。设备具有以下提示之一:

routerA#
rp/0/rps0/cpu0:routerB#
routerC> (enable)
routerD>

具有"#"的设备有一组命令,设备有"> (启用)"有一个不同的集合和那些只有">"得到第三组命令。鉴于我们在登录之前不知道提示是什么,我们需要有一个基于提示区分的脚本。

使用下面的代码,routerC和routerD可以工作,但是routerA和routerB没有。

#!/usr/local/bin/python3

import sys
import pexpect
from pexpect import pxssh
import time
import subprocess
import pdb

routerFile = open('test.hosts','r')
routeServers = [i for i in routerFile]
commandFile = open('standard.commands','r')
commands = [i for i in commandFile]
pdb.set_trace()
for router in routeServers:
  router = router.strip()
  print('####connecting to####', router)
  for i in range(max_retries):
    try:
      s = pxssh.pxssh()
      s.login(router,'username','password', original_prompt="\S+#$|\S+# $|\(\S+\)$|\(\S+\) $|\S+>$|\S+> $", auto_prompt_reset=False)
      print(s.before)
      print(s.after)
      pattern_index = s.expect([r'\S+#$', r'\S+# $', r'\S+> ', r'\S+>$'])
      print(s.before)
      print(s.after)
      if pattern_index == 0 or pattern_index == 1:
        print("#")        
      elif pattern_index == 2:
        print("catos")
       if pattern_index == 3:
        print("enable")
    except Exception as e:
      print ('!!!!!ERROR!!!!! Issue with host %s', router)
      print (str(e))
      s = None

这是我得到的调试输出。

> pxssh_test.py(21)<module>()
-> for router in routeServers:
(Pdb) n
> pxssh_test.py(22)<module>()
-> router = router.strip()
(Pdb)
> pxssh_test.py(23)<module>()
-> print('####connecting to####', router)
(Pdb)
####connecting to#### routerA
> pxssh_test.py(24)<module>()
-> for i in range(max_retries):
(Pdb)
> pxssh_test.py(25)<module>()
-> try:
(Pdb)
> pxssh_test.py(26)<module>()
-> s = pxssh.pxssh()
(Pdb)
> pxssh_test.py(27)<module>()
-> s.login(router,'username','password', original_prompt="\S+#$|\S+# $|\(\S+\)$|\(\S+\) $|\S+>$|\S+> $", auto_prompt_reset=False)
(Pdb)
> directory/pxssh_test.py(28)<module>()
-> print(s.before)
(Pdb)
b' \r\n....data removed....\r\n\r'
> directory/pxssh_test.py(29)<module>()
-> print(s.after)
(Pdb)
b'routerA# '
> pxssh_test.py(30)<module>()
-> pattern_index = s.expect([r'\S+#$', r'\S+# $', r'\S+> ', r'\S+>$'])
(Pdb)
pexpect.exceptions.TIMEOUT: Timeout exceeded.
<pexpect.pxssh.pxssh object at 0x7f9f19f8efd0>
command: /bin/ssh
args: ['/bin/ssh', '-q', '-l', 'intelliden', 'routerA']
buffer (last 100 chars): b''


after: <class 'pexpect.exceptions.TIMEOUT'>
match: None
match_index: None
exitstatus: None
flag_eof: False
pid: 42618
child_fd: 7
closed: False
timeout: 30
delimiter: <class 'pexpect.exceptions.EOF'>
logfile: None
logfile_read: None
logfile_send: None
maxread: 2000
ignorecase: False
searchwindowsize: None
delaybeforesend: 0.05
delayafterclose: 0.1
delayafterterminate: 0.1
searcher: searcher_re:
    0: re.compile("b'\\S+#$'")
    1: re.compile("b'\\S+# $'")
    2: re.compile("b'\\S+> '")
    3: re.compile("b'\\S+>$'")
> pxssh_test.py(30)<module>()
-> pattern_index = s.expect([r'\S+#$', r'\S+# $', r'\S+> ', r'\S+>$'])
(Pdb)
> pxssh_test.py(106)<module>()
-> except pexpect.EOF:
(Pdb)
> pxssh_test.py(113)<module>()
-> except pexpect.TIMEOUT:
(Pdb)

对于为什么#不起作用的任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

对于遇到同一问题的任何人,我必须添加一个sendline()并在pxssh.pxssh()和登录之间设置提示。

像这样:

...
s = pxssh.pxssh()
s.PROMPT = "\S+#|\S+# |(\S+)$|(\S+) $|\S+>$|\S+> $"
s.login(router,'username','password', auto_prompt_reset=False)
s.sendline()
pattern_index = s.expect([r'\S+#$', r'\S+# $', r'\S+> ', r'\S+>$'])
if pattern_index == 0 or pattern_index == 1:
...