首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Netmiko -进入配置终端模式

Netmiko -进入配置终端模式
EN

Stack Overflow用户
提问于 2021-11-21 11:59:30
回答 2查看 2.1K关注 0票数 0

我试图使用Netmiko和工作在‘配置终端’模式。为什么只有“启用”模式函数而没有“配置终端”模式。?(即: net_connect.enable())

我的程序应该执行以下步骤:

  1. 登录
  2. 更改为启用模式
  3. 更改为配置终端
  4. 插入受限许可证(在配置终端模式下)
  5. 运行“_shell”将从CLI更改为常规Linux。

编辑我尝试使用以下命令输入config_mode并获得'ValueError‘。

代码语言:javascript
复制
net_connect.find_prompt()
net_connect.enable()
net_connect.find_prompt()
net_connect.config_mode()
net_connect.find_prompt()

日志:

代码语言:javascript
复制
>>> 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. 

第二次尝试:

代码语言:javascript
复制
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()

谢谢,艾丽尔。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2021-11-21 12:14:22

为什么只有“启用”模式功能而不是“配置终端”模式。?

configure terminal中有一种netmiko模式,使用将configure terminal发送到远程设备。

但是,您可以使用send_config_set(),它默认进入配置模式,并在发送最后一个命令后退出。使用send_config_set()的一个例子

代码语言:javascript
复制
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)
代码语言:javascript
复制
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()的更多信息。

代码语言:javascript
复制
net_conn.send_command_timing("enable")
net_conn.send_command_timing("configure terminal")
票数 0
EN

Stack Overflow用户

发布于 2022-04-06 15:36:28

您必须确定用户的特权级别,或者使用秘密密码从ssh进入配置模式。

如果没有这个,

代码语言:javascript
复制
Conn.enable() & conn.send_config_set() 

不起作用

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70054323

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档