gpt4 book ai didi

python - Netmiko操作系统错误: Search pattern never detected in send_command:

转载 作者:行者123 更新时间:2023-12-02 02:02:49 27 4
gpt4 key购买 nike

我遇到了这个错误。

任何人都可以帮助解决这个错误吗?

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

最佳答案

send_command基于模式的。这意味着它会搜索设备提示以检测输出结束。对于 netmiko 中的 BaseConnection,每条命令完成的时间为 100 秒,但对于 Cisco 设备来说只有 10 秒,因为设置了 fast_cli 默认值为fast_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)

关于python - Netmiko操作系统错误: Search pattern never detected in send_command:,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68797284/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com