cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
1090
Views
2
Helpful
4
Replies

Netmiko for Switch macro creation

glsparks
Level 1
Level 1

Hi,

I'm attempting to write a netmiko script to create a macro on a Cisco switch.

However the script doesn't seem to like the fact that a prompt is being returned. 

Here is the relevant portion of the script:

switch1 = {
'device_type':'cisco_ios',
'host':hostIP,
'username':username,
'password':pwd,
'global_delay_factor':4,
'session_log':'netmiko.log'
}

#Define the commands to send

SwitchMacro = [
'macro name PythonTest',
'switchport host',
'switchport access vlan 999',
'shutdown',
'@'
]

# Apply the commands to the devices
all_devices = [switch1]

file = open('switchmacro.txt', 'w')
for devices in all_devices:

net_connect = ConnectHandler(**devices)

output = net_connect.send_config_set(SwitchMacro)
file.write(output)
#Print the verification to a file

file.close()

The log file shows:

SW1#configure terminal
Enter configuration commands, one per line. End with CNTL/Z.
SW1(config)#
SW1(config)#macro name PythonTest
Enter macro commands one per line. End with the character '@'.

Python output displays:

Pattern not detected: '(?:SW1.*$|#.*$)' in output.

Any ideas on how to resolve this?

1 Accepted Solution

Accepted Solutions

glsparks
Level 1
Level 1

Okay got this working now, rather by luck than by skill.

Added this and it started working:

output = net_connect.send_config_set(SwitchMacro, cmd_verify=False)

View solution in original post

4 Replies 4

Marcel Zehnder
Spotlight
Spotlight

Hi 

As soon as you send "macro name PythonTest" the prompt changes, and Netmiko can't handle this (Netmiko is looking for "#" before it sends the next command). You need to work with netmiko_send_command_timing or netmiko_send_command_expect to workaround this (attention in both cases your first command must be "conf t").

https://ktbyers.github.io/netmiko/docs/netmiko/index.html#netmiko.BaseConnection.send_command_expect
https://ktbyers.github.io/netmiko/docs/netmiko/index.html#netmiko.BaseConnection.send_command_timing

HTH

Hi,

Thanks for the info. 

I've tried both of those methods and i'm still not having any luck. Just get and attributeError 'list' object has no attribute 'rstrip'

on both. I didn't find the documentation particularly helpful to be honest. 

 

 

glsparks
Level 1
Level 1

Okay got this working now, rather by luck than by skill.

Added this and it started working:

output = net_connect.send_config_set(SwitchMacro, cmd_verify=False)

That‘s even better, didn‘t know that the flag behaves this way.