2

我被这个错误困住了。

任何人都可以帮助摆脱这个错误。

import netmiko 

Device = {"host":"xxxxxxxxxx", "device_type":"cisco_nxos", "username":"admin", "password":"xxxxxxxx"}

connect =netmiko.ConnectHandler(**Device)

connect.enable()

CLI = "show ip int bri"

print(connect.send_command(CLI))

CLI= "show run"

print(connect.send_command(CLI))

我得到了第一个 cmd 的结果,但我得到了第二个 cmd 的错误:

  "OSError: Search pattern never detected in send_command:"
4

2 回答 2

3

send_command基于模式的。这意味着它会搜索设备提示以检测输出结束。对于BaseConnectionin netmiko,每个命令完成的时间是 100 秒,但对于 Cisco 设备,只有 10 秒,因为fast_cli默认值设置为Truefast_cli简单地将 100 秒乘以 0.1 (100*0.1 = 10) 只是为了让它更快,但更快并不总是最好的选择。

您需要设置fast_cliFalse禁用超时。

在处理 Cisco 设备时始终设置fast_cli为、、或。Falsecisco_ioscisco_xecisco_xrcisco_nxosnetmiko v3.4.0

请尝试以下代码:

from netmiko import ConnectHandler

device = {
    "host": "xxxxxxxx",
    "device_type": "cisco_nxos",
    "username": "admin",
    "password": "xxxxxxxx",
    "fast_cli": False,  # Notice the item here
    "secret": "",  # Enable password (If applicable)
}

# Connect to the device
conn = ConnectHandler(**device)

# Check if connected in user mode and enter enable mode
# Make sure you set the "secret": "xxxx" in the device variable
if not conn.check_enable_mode():
    conn.enable()

intf_brief = conn.send_command("show interface brief")
run_cfg = conn.send_command("show running-config")

# Disconnect to clear the vty line
conn.disconnect()

# Do whatever you like with the outputs after the connection
# is terminated
print(intf_brief)
print(run_cfg)

另外的选择

您可以使用该send_command_timing方法而无需设置fast_clitoFalse而不是send_command. 前一种方法是基于延迟的。它不会搜索设备提示来检测输出结束,而是等待一段时间。

from netmiko import ConnectHandler

device = {
    "host": "xxxxxxxx",
    "device_type": "cisco_nxos",
    "username": "admin",
    "password": "xxxxxxxx",
    "secret": "",  # Enable password (If applicable)
}

# Connect to the device
conn = ConnectHandler(**device)

# Check if connected in user mode and enter enable mode
# Make sure you have the "secret": "xxxx" is set in device var
if not conn.check_enable_mode():
    conn.enable()

# Notice send_command_timing method here
intf_brief = conn.send_command_timing("show interface brief")
run_cfg = conn.send_command_timing("show running-config")

# Disconnect to clear the vty line
conn.disconnect()

# Do whatever you like with the outputs after the connection
# is terminated
print(intf_brief)
print(run_cfg)
于 2021-08-17T12:56:24.990 回答
1

我没有代表 > 49,所以我无法评论 Tes3awy 的回答,但想说声谢谢。我正在使用 cisco_xr 和 "fast_cli": False 设置解决了这个问题。有趣的是,我正在从两个不同的主机运行我的脚本。该错误在运行 netmiko 3.0.0 的系统上没有发生,但在运行 netmiko 3.4.0 的系统上确实发生了。

于 2021-08-20T13:23:08.620 回答