In our previous attempt we created a script that took a prepared text file containing switches ip addresses and ports to be switched to a dummy vlan, preventing access for anyone connecting without permission.
In this one, we will take a “Reclaim Unused Up” report that is created from Cisco Prime LMS, the well known and loved management platform for Cisco Network Equipment, and process it in order to turn unstructured text into structured data, containing a list of those switches and their interfaces that need to be reclaimed.
For our first effort, we will just try to store the relevant data in a list of lists (nested structures), ignoring all other text.
The first part of the code will process the csv and keep only the lines that interest us on a list of strings (lines). The csv is produced with the following format:
User Tracking
"Switch Port Report-Reclaim Unused Up Immediate Report as of 24 Jan 2019, 13:51:13 EET"
Device: Sw-xx-yy
Port,Port Name,Operational Status,Admin Status,Last Used
Fa0/14 , ,DOWN,UP,2016/03/18 12:02:27
Fa0/19 , ,DOWN,UP,2018/03/09 08:02:19
Fa0/20 , ,DOWN,UP,2014/02/03 12:33:16
Fa0/21 , ,DOWN,UP,2014/02/03 12:33:16
Fa0/22 , ,DOWN,UP,2014/02/03 12:33:16
Device: Sw-zz-ww
Port,Port Name,Operational Status,Admin Status,Last Used
Gi1/0/2 , ,DOWN,UP,2016/01/20 12:07:06
Gi1/0/4 , ,DOWN,UP,2016/01/20 12:07:06
Gi1/0/6 , ,DOWN,UP,2016/01/20 12:07:06
Gi1/0/7 , ,DOWN,UP,2016/01/20 12:07:06
etc.
So the first lines are of no importance, then we have the Device name, then the column titles (again not useful) and then the list of interfaces accompanied by a list of fields for each one, with the description, the op status, the admin status and when it was last used
Our code fist separates the useful data from the rest and puts it into a list. Then that list is again processed. The Device name is stored and then from the subsequent lines, only the interface name is stored for each line. We use ‘if’ blocks to decide whether to store the listed data for each switch or append the switch to an outer list (switchlist) when we are done creating it. In this way a list of switches is created and each switch is a list as well, containing the device name and a series of interfaces.
We then print the stored information using nested ‘For’ loops to iterate across all stored items. Here is the code so far:
import os
import subprocess
import re
import sys
linelist = []
#open the switch list file
with open("export.csv") as switchfile:
for nextline in switchfile:
rawline = nextline.strip()
if (rawline == "") or ("port" in rawline) or ("User" in rawline) or ("Port" in rawline):
continue
linelist.append(rawline)
linelist.append("End")
switchlist = []
firstswitch = True
devicename = ""
switch = []
for line in linelist:
devicept = re.compile(r"Device:\s+(?P<devicename>\S+)")
devicesearch = devicept.search(line)
if devicesearch != None:
if firstswitch == False:
switchlist.append(switch)
devicename = devicesearch.group('devicename')
switch = []
switch.append(devicename)
if firstswitch == True:
firstswitch = False
elif "End" in line:
switchlist.append(switch)
break
else:
interlist = line.split(',')
switch.append(interlist[0])
for switch in switchlist:
for item in switch:
print (item)
print ("done")
Our script can of course be made better by also using dictionaries for each switch. It also lacks more info to be used in a fully automated way, like the ip address for the switches.
What do you think? If we add the ip address using a space after the Device Name, can you modify the script so that the ip address is separated and stored as well? In that case we could use the ip for each switch to connect to it and change the access vlan for each port. Send me a link to your answer to my twitter account @mythryll by twitting to me or sending me a dm (you will probably need to store the text somewhere, it will be too large for twitter).
Tomorrow, Jan 29, is the Official 1rst day for Cisco Live Europe 2019 (CLEUR), in Barcelona, Spain. Go by the Cisco Devnet Zone, find the Start Now area and start learning how to start with Network Programming and leverage your skills to solve everyday problems and automate boring and repetitive work!
You can also take a look at this link and provide an answer about how to store the data we saved into a text file for further processing. https://realpython.com/working-with-files-in-python/
Send me that too, if you like. I will post the best answer here and mention the author of the answer on twitter.
Remember the challenge is for novice python coders that are mostly network engineers. The experienced ones will probably be bored with this. It’s not for them.
I promise to be back for more.
JT.