繁体   English   中英

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

[英]Python pxssh expect pattern is not matching '#'

我正在尝试使用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)

对于为什么#无效的任何帮助,将不胜感激。

对于遇到相同问题的任何人,我都必须添加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:
...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM