【问题标题】:Netmiko commit method is not committing and do not exit from config modeNetmiko 提交方法未提交且不退出配置模式
【发布时间】:2021-12-25 01:01:22
【问题描述】:

我是新的 netmiko 库,但文档似乎相当直截了当,不确定我的代码中缺少什么(或我不确定的错误)导致提交无法在 Cisco ios 上通过。

device = ConnectHandler(device_type="cisco_ios",
                                host="cisco-lab-device",
                                username="admin",
                                password="passwd",
                                session_timeout=120,
                                verbose=True)
                                #session_log="output.txt")
cmdlist = ['vrf new']
try:
    result = device.send_config_set(cmdlist,exit_config_mode=False)
    print("Result after sending commands ", result)
except Exception as err:
    device.disconnect()
    return

result = device.commit()
print('DONE') # I dont see this print at all
device.disconnect()

##This is the dump upon running the code:

Result after sending commands

RP/0/RP0/CPU0:cisco-lab-device#

RP/0/RP0/CPU0:cisco-lab-device#

RP/0/RP0/CPU0:cisco-lab-device#configure terminal

RP/0/RP0/CPU0:cisco-lab-device(config)#vrf new

RP/0/RP0/CPU0:cisco-lab-device(config-vrf)#

正如我上面提到的,我没有看到我在提交方法之后添加的打印跟踪。而且我还检查了 cisco box 上的 show running-config 看起来提交没有通过,并且没有反映新的 vrf 创建。我尝试了一堆其他命令,但它也卡在了同一点。有人可以阐明我在这里可能缺少什么吗??

【问题讨论】:

  • 请使用cisco_xr 而不是cisco_ios。后者没有要提交的候选配置概念。

标签: python automation cisco sdn netmiko


【解决方案1】:

尝试在此代码的基础上进行构建。

这是一个简单的代码,它将获取设备 ip、用户名、密码和设备类型。它将连接到设备并发送您在 config_commands 列表中键入的命令。它将发送命令并打印输出。而且你可以沙化单个命令,只需将它放入 send_command("example_command")

from netmiko import ConnectHandler
from getpass import getpass
password = getpass()
RTR_150 = {
    'ip':   '150.154.1.10',
    'username': 'test',
    'password': 'test',
    'device_type': 'cisco_ios',
}

net_connect = ConnectHandler(**RTR_150)

config_commands = [ 'int lo0',
                    'ip add 1.1.1.1 255.255.255.0',
                    'no shut' ]
output = net_connect.send_config_set(config_commands)
print(output)

output = net_connect.send_command('show ip int brief')
print(output)

【讨论】:

  • 感谢您的建议。我发现 device_type 应该是 cisco_xr 而不是 cisco_ios。毕竟它的 XR,我忽略了。
【解决方案2】:

正如您已经指出的那样,Cisco IOS 没有提交操作的概念 - 命令会立即提交。 IOS XR 应在 Netmiko device_type 参数中指定。

【讨论】:

  • 正如目前所写,您的答案尚不清楚。请edit 添加其他详细信息,以帮助其他人了解这如何解决所提出的问题。你可以找到更多关于如何写好答案的信息in the help center。
【解决方案3】:

这个配置可以帮助你:

from netmiko import ConnectHandler
from getpass import getpass

RTR_150 = {
    "device_type": "cisco_ios",
    "host": "router01",
    "username": "test",
    "password": "test",
    "secret": "secret_if_you_need"}

with ConnectHandler(**RTR_150) as net_connect:
    config_commands = [ "int loo 0",
                        "ip add 1.1.1.1 255.255.255.0",
                        "no shut",
                        "do show ip int brief"]

    net_connect.enable()
    output = net_connect.send_config_set(config_commands)
    net_connect.disconnect()
    
    print(output)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-12-14
    • 1970-01-01
    • 1970-01-01
    • 2023-03-25
    • 2014-02-03
    • 2019-01-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多