Browse By

Python Automation Script | Get Cisco Routers and Switches details in bulk

Run Python Automation Script to fetch Cisco Routers and Switches details in Bulk

Today in this article we will see how to use a python script to get Cisco router and Cisco switch status in bulk. The details includes “hostname, uptime, current version, current image, serial number, device model, device memory

Pre-Requisite is to install Netmiko. In case you have not installed netmiko, follow our previous post:

Netmiko SSH – Python Automation on Cisco Routers and Switches using SSH

What the script is doing (Trying to explain in very short)?

  • The script will login to each IP Address defined in ip_list
  • Create 2 files to save the output of the script (fetch data and write to IOS.csv on successful login and write to login_issues.csv on unsuccessful login with reason)
  • In case of any exception/error, it will not stop the script, it will write the error/exceptions in login_issues.csv and continue to run the script for the remaining IP Address.
  • It will use regex to fetch all the information from router/switch like “uptime, hostname, version, memory, ios, serial, model etc
  • All the data will be appended to devices = []
  • Finally it will write the data from devices = [] to IOS.csv file.

Here is the working Python script:

from netmiko import ConnectHandler
from netmiko.ssh_exception import NetMikoTimeoutException
from netmiko.ssh_exception import SSHException
from netmiko.ssh_exception import AuthenticationException

import re

#here is list of cisco routers ip addresses
ip_list = ['', '', '', '']

#list where informations will be stored
devices = []

#clearing the old data from the CSV file and writing the headers
f = open("IOS.csv", "w+")
f.write("IP Address, Hostname, Uptime, Current_Version, Current_Image, Serial_Number, Device_Model, Device_Memory")

#clearing the old data from the CSV file and writing the headers
f = open("login_issues.csv", "w+")
f.write("IP Address, Status")

#loop all ip addresses in ip_list
for ip in ip_list:
	cisco = {
	'username':'cisco',     #ssh username
	'password':'cisco',  #ssh password
	'secret': 'cisco',   #ssh_enable_password
	#handling exceptions errors
		net_connect = ConnectHandler(**cisco)
	except NetMikoTimeoutException:
		f = open("login_issues.csv", "a")
		f.write(ip + "," + "Device Unreachable/SSH not enabled")
	except AuthenticationException:
		f = open("login_issues.csv", "a")
		f.write(ip + "," + "Authentication Failure")
	except SSHException:
		f = open("login_issues.csv", "a")
		f.write(ip + "," + "SSH not enabled")


	#handling exceptions errors		
	except ValueError:
		f = open("login_issues.csv", "a")
		f.write(ip + "," + "Could be SSH Enable Password issue")

	# execute show version on router and save output to output object	
	sh_ver_output = net_connect.send_command('show version')   

	#finding hostname in output using regular expressions
	regex_hostname = re.compile(r'(\S+)\suptime')
	hostname = regex_hostname.findall(sh_ver_output)

	#finding uptime in output using regular expressions
	regex_uptime = re.compile(r'\S+\suptime\sis\s(.+)')
	uptime = regex_uptime.findall(sh_ver_output)
	uptime = str(uptime).replace(',' ,'').replace("'" ,"")
	uptime = str(uptime)[1:-1] 
	#finding version in output using regular expressions
	regex_version = re.compile(r'Cisco\sIOS\sSoftware.+Version\s([^,]+)')
	version = regex_version.findall(sh_ver_output)

	#finding serial in output using regular expressions
	regex_serial = re.compile(r'Processor\sboard\sID\s(\S+)')
	serial = regex_serial.findall(sh_ver_output)

	#finding ios image in output using regular expressions
	regex_ios = re.compile(r'System\simage\sfile\sis\s"([^ "]+)')
	ios = regex_ios.findall(sh_ver_output)

	#finding model in output using regular expressions
	regex_model = re.compile(r'[Cc]isco\s(\S+).*memory.')
	model = regex_model.findall(sh_ver_output)

	#finding the router's memory using regular expressions
	regex_memory ='with (.*?) bytes of memory', sh_ver_output).group(1)
	memory = regex_memory
	#append results to table [hostname,uptime,version,serial,ios,model]
	devices.append([ip, hostname[0],uptime,version[0],ios[0], serial[0],model[0], memory])

#print all results (for all routers) on screen    
for i in devices:
	i = ", ".join(i)	  
	f = open("IOS.csv", "a")

Modify the script as per your requirement:

  • Modify the ip_list  according to your requirement. (See below)
  • Modify Username, Password and Secret according to your requirement. (See below)

  • Run the python script and it will generate two files upon completion i.e., IOS.csv and login_issues.csv.
  • IOS.csv – All the data that will be successfully fetched from the rotuers/switches will be stored here.
  • login_issues.csv – All the unsuccessful login (like device unreachable, authentication error) will be captured here.

Here is the output of the Python script execution:

The below screenshot shows the data successfully fetched and the data written to IOS.csv

The below screenshot shows that the login was unsuccessful due to the device not reachable or SSH not enabled or Authentication issues and the reason/error is written in login_issues.csv.


On What devices this script has been tested?

This script has been tested on Cisco 2811, Cisco 2901, Cisco 2911, Cisco 3725, Cisco 3745, Cisco 3825, Cisco 3925, Cisco ISR4221 and ISR4451 and it works perfectly fine.

Would love to hear back from you on what devices you have tested and where it has worked or not worked.


Leave a Reply

Your email address will not be published. Required fields are marked *