Automating Cisco Router/Switch changes with Python and Netmiko

Jacob DurhamLead Network and System Administrator
CERTIFIED EXPERT
Systems Administrator
Published:
Edited by: Andrew Leniart
A guide on the process of automating change or gathering data from multiple Cisco IOS devices. If this can help you - please use it. God knows we all need a few extra minutes in our day.

We're migrating DataCenters. Anyone who has been involved with a migration knows it's not a fun task. We're also migrating subnets which makes the fun increase exponentially.


One of the tasks I had today was to change the IP address where our Cisco routers are sending their error and system logs. In total, this is only a few commands per device and probably a 20-minute task. However, I'm teaching myself Python and wanted to both (a) learn something new and (b) create something that would make future changes easier.


Between Google and trial/error I've put together this process of automating change or gathering data from multiple Cisco IOS devices. If this can help you - please use it. God knows we all need a few extra minutes in our day.


Setup


First, you'll need Python 3. I'm not going to go into detail on how to install that but here's an excellent guide.


Next, you'll need 2 files. These will need to be in the same folder/directory as your script.


iplist.txt - which will be a list of the IP addresses that you want to connect to.


configfile.txt - which will be the lines of config that you want to push to the routers.



The Script


##imports python modules needed to work
from netmiko import ConnectHandler
import time, sys, getpass, paramiko 

 
##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.
device = {     'device_type': 'cisco_ios',     'ip': '192.168.43.10',     'username': 'username',     'password': 'password',     'secret':'password'
    } 

 
ipfile=open("iplist.txt")
print ("If you've not edited configfile.txt please stop and figure out what you're about to do...") device['username']=input("Enter your SSH username:  ") device['password']=getpass.getpass() device['secret']=input("Enter the enable password: ") configfile=open("configfile.txt") configset=configfile.read() ##reads the config file
configfile.close() 

for line in ipfile:  
 device['ip']=line.strip("\n")  print("\n\nConnecting Device.. ",line)  net_connect = ConnectHandler(**device)  net_connect.enable()  time.sleep(2)  print ("Passing configuration set ")  output = net_connect.send_config_set(configset)  print(output)  print ("Device Conigured ")  
 
ipfile.close()

There isn't anything you need to edit inside the script. You can add/remove devices using the iplist.txt and you can change your commands using the configfile.txt.


The Outcome


Before we run our script.

test-rtr1#show run | i logging 10.199
test-rtr1#

Then we run it.

jacoby@jacoby-python:~/Documents$ python3 routers.py  If you've not edited configfile.txt please stop and figure out what you're about to do... Enter your SSH username:  admin Password:  Enter the enable password: thisisntmyenablepassword 

 
Connecting Device..  192.168.1.254
 
Passing configuration set  config term Enter configuration commands, one per line.  End with CNTL/Z. tuk-rtr1(config)#logging 10.199.0.87
tuk-rtr1(config)#end
tuk-rtr1#write mem
Building configuration... 

Device Conigured  jacoby@jacoby-python:~/Documents$  


And the outcome of running our script

test-rtr1#show run | i logging 10.199
logging 10.199.0.87
test-rtr1#


Conclusion


This obviously is quite a bit of work to go through for a single router but as the number of devices increases, so does the time saved.


I hope this helps you save a few minutes of your day or at least learn a bit about Python.

1
3,144 Views
Jacob DurhamLead Network and System Administrator
CERTIFIED EXPERT
Systems Administrator

Comments (2)

SouljaSr.Net.Eng
CERTIFIED EXPERT
Top Expert 2011

Commented:
Thanks Jacob! This was definitely helpful!
SouljaSr.Net.Eng
CERTIFIED EXPERT
Top Expert 2011

Commented:
Here is the script I put together with your help from this article. Thanks!!

##imports python modules needed to work
from netmiko import ConnectHandler
import time, sys, getpass, paramiko

##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 = raw_input("Enter your SSH username: ")
pword = getpass.getpass()

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)

Open in new window

Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.

Get access with a 7-day free trial.
You Belong in the World's Smartest IT Community