Netmiko 发送命令失败

问题描述 投票:0回答:1

我有一个脚本,可以很好地登录交换机并获取所有接口描述符,并制作一个脚本在接口级别添加 2 个陷阱。但是,当我尝试将该多行字符串存储在变量中并将其发送回设备时,我收到错误。

import re
import netmiko

from netmiko import ConnectHandler

# Define the device information

device = {

    'device_type': 'cisco_ios',

    'ip': '10.10.10.103',

    'username': 'cisco',

    'password': 'cisco',

    'port': 22,

}

# Connect to the device

with ConnectHandler(**device) as net_connect:

 # Retrieve the interfaces
    output = net_connect.send_command('sh ip int bri')

 

    # Search to isolate interface descriptor with regex

matches = re.findall(r'^\S+', output, flags=re.M)


for i in range(len(matches)):
    matches[i] = "interface " + str(matches[i]) +  " \n snmp trap mac-notification change added  \n snmp trap mac-notification change removed\n" 
result = " ".join(matches)

conf_matches = "conf t\n" + result
print(conf_matches)

with ConnectHandler(**device) as net_connect:
    net_connect.send_command(conf_matches)

输出

会议 接口以太网0/0 添加了 snmp trap mac 通知更改
snmp 陷阱 mac 通知更改已删除 接口以太网0/1 添加了 snmp trap mac 通知更改
snmp 陷阱 mac 通知更改已删除 接口以太网0/2 添加了 snmp trap mac 通知更改
snmp 陷阱 mac 通知更改已删除 接口以太网0/3 添加了 snmp trap mac 通知更改
snmp 陷阱 mac 通知更改已删除

回溯(最近一次调用最后一次): 文件“/Users/netcompute/Desktop/netmiko/main.py”,第 56 行,位于 net_connect.send_command(conf_matches) 文件“/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/netmiko/utilities.py”,第592行,在wrapper_decorator中 返回 func(self, *args, **kwargs) 文件“/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/netmiko/base_connection.py”,第 1671 行,在 send_command 中 new_data = self.command_echo_read(cmd=cmd, read_timeout=10) 文件“/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/netmiko/base_connection.py”,第 1396 行,在 command_echo_read 中 new_data = self.read_until_pattern( 文件“/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/netmiko/base_connection.py”,第 672 行,在 read_until_pattern 中 提高读取超时(msg) netmiko.exceptions.ReadTimeout:

您可以尝试解决此问题的方法:

  1. 调整正则表达式模式以更好地识别终止字符串。注意,在 很多情况下,模式是根据网络设备的提示自动进行的。
  2. 将 read_timeout 增加到更大的值。

您还可以查看 Netmiko session_log 或调试日志以获取更多信息。

错误-(您可以看到字符串打印得很完美,但是当它第二次尝试使用 netmiko 发送字符串时,它出错了。)

以上公开供大家阅读

automation cisco netmiko
1个回答
0
投票

思科设备不接受多行字符串作为输入。它们一次获取一行并返回每行的输出。要发送一组配置命令,您需要为单个命令调用

send_command
。或者您可以将
send_config_set
与可迭代的配置命令列表一起使用。

net_connect.send_config_set(conf_matches.splitlines())
© www.soinside.com 2019 - 2024. All rights reserved.