簡體   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