我试图使用Netmiko和工作在‘配置终端’模式。为什么只有“启用”模式函数而没有“配置终端”模式。?(即: net_connect.enable())
我的程序应该执行以下步骤:
编辑我尝试使用以下命令输入config_mode并获得'ValueError‘。
net_connect.find_prompt()
net_connect.enable()
net_connect.find_prompt()
net_connect.config_mode()
net_connect.find_prompt()日志:
>>> from netmiko import ConnectHandler
>>> net_connect = ConnectHandler(device_type='cisco_ios', host='r-hpc-sw19', username='admin', password='admin')
>>> net_connect.find_prompt()
'r-hpc-sw19 [standalone: master] >'
>>> net_connect.enable()
'enable\r\r\n\rr-hpc-sw19 [standalone: master] # '
>>> net_connect.find_prompt()
'r-hpc-sw19 [standalone: master] #'
>>> net_connect.config_mode()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/labhome/arielwe/.local/lib/python3.9/site-packages/netmiko/cisco_base_connection.py", line 48, in config_mode
return super().config_mode(
File "/labhome/arielwe/.local/lib/python3.9/site-packages/netmiko/base_connection.py", line 1766, in config_mode
raise ValueError("Failed to enter configuration mode.")
ValueError: Failed to enter configuration mode. 第二次尝试:
from netmiko import ConnectHandler
net_connect = ConnectHandler(device_type='cisco_ios', host='r-hpc-sw19', username='admin', password='admin')
net_connect.find_prompt()
net_connect.enable()
net_connect.find_prompt()
cfg = net_connect.send_config_set(["show version | json-print"])
net_connect.find_prompt()
>>> from netmiko import ConnectHandler
>>> net_connect = ConnectHandler(device_type='cisco_ios', host='r-hpc-sw19', username='admin', password='admin')
>>> net_connect.find_prompt()
'r-hpc-sw19 [standalone: master] >'
>>> net_connect.enable()
'enable\r\r\n\rr-hpc-sw19 [standalone: master] # '
>>> net_connect.find_prompt()
'r-hpc-sw19 [standalone: master] #'
>>> cfg = net_connect.send_config_set(["show version | json-print"])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/labhome/arielwe/.local/lib/python3.9/site-packages/netmiko/base_connection.py", line 1876, in send_config_set
output += self.config_mode(*cfg_mode_args)
File "/labhome/arielwe/.local/lib/python3.9/site-packages/netmiko/cisco_base_connection.py", line 48, in config_mode
return super().config_mode(
File "/labhome/arielwe/.local/lib/python3.9/site-packages/netmiko/base_connection.py", line 1766, in config_mode
raise ValueError("Failed to enter configuration mode.")
ValueError: Failed to enter configuration mode.
>>> net_connect.find_prompt()谢谢,艾丽尔。
发布于 2021-11-21 12:14:22
为什么只有“启用”模式功能而不是“配置终端”模式。?
在configure terminal中有一种netmiko模式,使用将configure terminal发送到远程设备。
但是,您可以使用send_config_set(),它默认进入配置模式,并在发送最后一个命令后退出。使用send_config_set()的一个例子
from netmiko import ConnectHandler
device = {
"device_type": "cisco_ios",
"ip": "192.168.1.1",
"username": "cisco",
"password": "cisco",
"secret": "cisco",
"fast_cli": False,
}
with ConnectHandler(**device) as conn:
if not conn.check_enable_mode():
conn.enable()
cfg = conn.send_config_set(["interface Loopback 212", "shutdown"])
print(cfg)RTR1(config)#configure terminal
Enter configuration commands, one per line. End with CNTL/Z.
RTR1(config)#interface Loopback 212
RTR1(config-if)#shutdown
RTR1(config-if)#end
RTR1#编辑
由于send_config_set()和config_mode()都不起作用,所以您可以使用send_command_timing()自己编写命令,而不是使用netmiko中的方法。
send_command_timing()是基于延迟的,而不是send_command()那样的。定时方法等待某个时间,然后发送命令,它不搜索任何模式。您还可以阅读本文档链接的第一部分,以了解有关send_command_timing()的更多信息。
net_conn.send_command_timing("enable")
net_conn.send_command_timing("configure terminal")发布于 2022-04-06 15:36:28
您必须确定用户的特权级别,或者使用秘密密码从ssh进入配置模式。
如果没有这个,
Conn.enable() & conn.send_config_set() 不起作用
https://stackoverflow.com/questions/70054323
复制相似问题