我正在使用下面的Python代码来获取show version的输出
from netmiko import ConnectHandler
device_cisco_ios = {
'device_type': 'cisco_ios',
'host': "ABCDEF32",
'username': "XXXX",
'password': "xxxxx",
"session_log": 'netmiko_session.log',
}
try:
with ConnectHandler(**device_cisco_ios) as ssh:
cmd = ssh.send_command("sh ver")
print(cmd)
except Exception as e:
print(str(e))
以下是“netmiko_session.log”文件内容
Last login: Fri Oct 27 17:57:36 2023 from 10.245.246.24
terminal width 511
terminal length 0
RP/0/RP0/CPU0:ABCDEF32#
RP/0/RP0/CPU0:ABCDEF32#terminal width 511
RP/0/RP0/CPU0:ABCDEF32#terminal length 0
sh ver
RP/0/RP0/CPU0:ABCDEF32#
RP/0/RP0/CPU0:ABCDEF32#
RP/0/RP0/CPU0:ABCDEF32#
RP/0/RP0/CPU0:ABCDEF32#
RP/0/RP0/CPU0:ABCDEF32#
RP/0/RP0/CPU0:ABCDEF32#sh ver
Cisco IOS XR Software, Version 7.3.3 LNT
Copyright (c) 2013-2022 by Cisco Systems, Inc.
Build Information:
Built By : xxxx
Built On : xxx
Build Host : xxx
Workspace : XXXX
Version : 7.3.3
Label : 7.3.3
cisco 8000 (Intel(R) Xeon(R) CPU D-1530 @ 2.40GHz)
cisco 8201-32FH (Intel(R) Xeon(R) CPU D-1530 @ 2.40GHz) processor with 32GB of memory
ABCDEF32 uptime is 24 weeks, 11 hours, 15 minutes
Cisco 8000 Series 32x400G QSFPDD 1RU Fixed System w/HBM
RP/0/RP0/CPU0:ABCDEF32#
RP/0/RP0/CPU0:ABCDEF32#exit
下面是我遇到的异常
Pattern not detected: 'RP/0/RP0/CPU0:ABCDEF32\\#terminal\\ length\\ 0' in output.
Things you might try to fix this:
1. Explicitly set your pattern using the expect_string argument.
2. Increase the read_timeout to a larger value.
You can also look at the Netmiko session_log or debug log for more information.
我不确定为什么在没有明确指定的情况下无法检测到模式,并且同一设备有时工作正常,没有任何异常,我正在使用 netmiko 4.2.0。如果有人能帮助我,那就太好了。
我会为 IOS XR 设备使用正确的驱动程序,因为这就是您与之交互的设备。
'device_type': 'cisco_xr',
驱动程序处理提示检测的方式与 IOS 设备略有不同。