send_command是基于模式的。这意味着它会搜索设备提示以检测输出结束。对于BaseConnectionin netmiko,每个命令完成的时间是 100 秒,但对于 Cisco 设备,只有 10 秒,因为fast_cli默认值设置为True。fast_cli简单地将 100 秒乘以 0.1 (100*0.1 = 10) 只是为了让它更快,但更快并不总是最好的选择。
您需要设置fast_cli为False禁用超时。
在处理 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)