Reputation: 1
I have a script that works well to log inot a switch and grab all the interface descriptors and make a script to add 2 traps at the interface level. However when I try to store that multi-line string in a variable and send it back to the device I get errors.
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)
OUTPUT
conf t
interface Ethernet0/0
snmp trap mac-notification change added
snmp trap mac-notification change removed
interface Ethernet0/1
snmp trap mac-notification change added
snmp trap mac-notification change removed
interface Ethernet0/2
snmp trap mac-notification change added
snmp trap mac-notification change removed
interface Ethernet0/3
snmp trap mac-notification change added
snmp trap mac-notification change removed
Traceback (most recent call last): File "/Users/netcompute/Desktop/netmiko/main.py", line 56, in net_connect.send_command(conf_matches) File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/netmiko/utilities.py", line 592, in wrapper_decorator return func(self, *args, **kwargs) File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/netmiko/base_connection.py", line 1671, in send_command new_data = self.command_echo_read(cmd=cmd, read_timeout=10) File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/netmiko/base_connection.py", line 1396, in command_echo_read new_data = self.read_until_pattern( File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/netmiko/base_connection.py", line 672, in read_until_pattern raise ReadTimeout(msg) netmiko.exceptions.ReadTimeout:
Things you might try to fix this:
You can also look at the Netmiko session_log or debug log for more information.
ERRORS- (you can see the string gets printed perfectly but when it trys to send the string using netmiko the second time it errors.)
Above disclosed for all to read
Upvotes: 0
Views: 232
Reputation: 21
Cisco devices don't take multi-line strings as input. They take a single line at a time and return output for each line. To send a group of configuration commands, you need to call send_command
for individual command. Or you can use send_config_set
with an iterable list of configuration commands.
net_connect.send_config_set(conf_matches.splitlines())
Upvotes: 0