[英]Netmiko library returns pattern never detected in send_command_expect
[英]Netmiko Pattern Not Detected
我用 cisco 2960X 交換機運行了這段代碼。
from netmiko import ConnectHandler
network_device= {
"host": "192.168.xxx.xxx",
"username": "xxxx",
"password": "xxxx",
"device_type": "cisco_ios",
"session_log": "netmiko_session.log"
}
connect= ConnectHandler(**network_device)
connect.enable()
interface_name= "GigabitEthernet1/0/10"
def send_command(command):
return connect.send_command(command)
try:
send_command('enable')
send_command('configure terminal')
except Exception as e:
print(e)
print("Failed!")
但是在我遇到以下錯誤之后。
Pattern not detected: 'Switch\\#' in output.
Things you might try to fix this:
1. Explicitly set your pattern using the expect_string argument.
2. Increase the read_timeout to a larger value.
You can also look at the Netmiko session_log or debug log for more information.
Failed!
請檢查下面的 netmiko_session.log
Switch#
Switch#terminal width 511
Switch#terminal length 0
Switch#
Switch#enable
Switch#
Switch#configure terminal
Enter configuration commands, one per line. End with CNTL/Z.
Switch(config)#
我重命名了交換機主機名。 但也有同樣的錯誤。
如果你想做配置更改,你應該考慮使用ssh.send_config_set
方法:
from netmiko import ConnectHandler
network_device= {
"host": "192.168.xxx.xxx",
"username": "xxxx",
"password": "xxxx",
"device_type": "cisco_ios",
"session_log": "netmiko_session.log"
}
connect = ConnectHandler(**network_device)
config_commands = [
'hostname NEW_NAME',
'interface Gi1/0/10',
'description NEW_IF_NAME'
]
connect.enable()
connect.send_config_set(config_commands)
connect.save_config()
connect.disconnect()
當您發送命令“conf terminal”時,提示符會更改以反映配置模式,它開始看起來像Switch(config)#
而 netmiko 仍在等待Switch#
所以或者,如果你想一個一個地發送命令,你可以設置預期的提示或使用“send_command_timing”
# this one will just wait 2 seconds before thinking that the command was applied to the device
connect.send_command_timing('hostname NEW_NAME', read_timeout=2)
# this one will expect a custom string from the device after the command is entered
connect.send_command('hostname NEW_NAME', expect_string='(config)#')
# be aware, that if you enter "configure interface" mode prompt will also change
connect.send_command('interface gi1/0/10', expect_string='(config-if)#')
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.