Python pxsh expect pattern与“#”不匹配

2024-09-28 19:05:56 发布

您现在位置:Python中文网/ 问答频道 /正文

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

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

带“#”的设备有一组命令,“>;(enable)”的设备有一组不同的命令,只有“>;”的设备有第三组命令。假设在登录之后我们才知道提示是什么,我们需要一个基于提示进行区分的脚本。在

下面是我的代码,routerC和routerD可以工作,但是routerA和routerB不行。在

^{pr2}$

这是我得到的调试输出。在

> 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)

如果您能提供任何帮助,说明为什么不能使用,我们将不胜感激。在


Tags: pytest命令renonefalseindexpdb
1条回答
网友
1楼 · 发布于 2024-09-28 19:05:56

对于遇到相同问题的任何人,我必须添加sendline()并将提示设置为pxsh.pxsh文件()和登录。在

像这样:

...
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:
...

相关问题 更多 >