ā03-21-2023 04:37 AM
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?
Solved! Go to Solution.
ā03-21-2023 08:34 AM
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)
ā03-21-2023 04:59 AM
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
ā03-21-2023 07:53 AM
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.
ā03-21-2023 08:34 AM
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)
ā03-21-2023 09:04 AM - edited ā03-21-2023 11:16 PM
Thatās even better, didnāt know that the flag behaves this way.
Discover and save your favorite ideas. Come back to expert answers, step-by-step guides, recent topics, and more.
New here? Get started with these tips. How to use Community New member guide