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.
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.
##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.
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#
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.
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.
Comments (2)
Commented:
Commented:
Open in new window