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

标签 python netmiko

我遇到了这个错误。

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

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/

相关文章:

python - 我可以在 python 的列表列表中扩展吗?

python - 在 Python 文件对象上使用迭代器时是否需要 close()

python - Django Twoscoops 和 SECRET_KEY 生成

python - 如何告诉 mypy 包含 stub 文件

python - 避免重复的随机值

python - 正则表达式,包括匹配 juniper srx 输出的变量

python - 思科设备中的Netmiko SSH连接问题

Python:检测文本文件中是否只出现给定的单词之一