Netmiko OSError:在send_命令中从未检测到搜索模式:

2024-06-01 07:16:16 发布

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

我被这个错误缠住了

谁能帮我摆脱这个错误呢

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

我得到第一个命令的结果,但第二个命令的错误如下:

  "OSError: Search pattern never detected in send_command:"

Tags: import命令sendhostclidevicetypeshow
2条回答

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

您需要将fast_cli设置为False以禁用超时

Always set fast_cli to False when dealing with Cisco devices either cisco_ios, cisco_xe, cisco_xr, or cisco_nxos in netmiko 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_cli设置为False而不是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)

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

相关问题 更多 >