10-22-2020 06:04 AM
So i have a python script that applies configs to devices and that part works no problem. The problem is when it hits an exception the script dies. I have looked at a bunch of python resources and I just can't figure it out. Any help would be awesome!
##imports python modules needed to work
from netmiko import ConnectHandler
import time, sys, getpass, paramiko
from netmiko.ssh_exception import NetMikoTimeoutException
from netmiko.ssh_exception import NetMikoAuthenticationException
##selects the correct Netmiko class based upon the device_type.
## I then define a network device dictionary consisting of a device_type, ip, username, and password.
user = ""
pword = ""
device = {
'device_type': 'cisco_ios',
#'ip': '192.168.43.10',
'username': user,
'password': pword,
#'secret':'password'
}
ipfile=open("iplist.txt") #This file contains a list of switch ip addresses.
#print ("Please doublecheck your configuration in the config file. Please stop and figure out what you're about to do...")
configfile=open("configfile.txt") #opening the config file with the changes you want to push
configset=configfile.read() ##reads the config file
configfile.close() #closes the config file
for line in ipfile:
device['ip']=line.strip()
print("Connecting to Device " + line)
net_connect = ConnectHandler(**device)
time.sleep(2)
print ("Applying Configuration to Device " + line)
output = net_connect.send_config_set(configset)
print(output)
10-22-2020 06:54 AM
Netmiko will send the commands that you specify down the SSH channel. You will need to look at the output of what comes back to see what happened. In your above case, it should report you back as % invalid command from the remote device (if is the invalid config you are looking for?) Netmiko doesn't sanity check the validity of your configuration commands, nor the response returned by the device.
Typically I would use the try
and except
block in Python, this is used to catch and handle exceptions. For example, Python executes code following the statement as a “normal” part of the program. The code that follows the statement is the program’s response to any exceptions in the preceding try clause. For example, you use this if the device was unreachable.
Hope this helps.
10-22-2020 10:27 AM
thank you i will give that a try
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