Traditionally, us Network Engineers have had to live at the mercy of the command line for performing pretty much any task in our day-to-day work lives. Let’s say for example, that you want to verify the routing table of a couple routers. Conventional wisdom tells us that to accomplish that, we need to login to each router over SSH using a terminal emulator like Putty and type ‘show ip route’. Easy stuff! What if, instead of a couple routers, you have to do that on 10 routers… or how about a 100? You are likely to spend the next couple hours doing just that… login, check output, repeat!
Well, turns out that there is a much easier way to get that accomplished.
Below is a relatively simple Python script which you can use to view the ‘show’ outputs of devices like Routers and Switches and comprising of a range of vendors. You don’t have to be an expert at using Python; just the ability to run a Python code is more than sufficient. The whole idea is to use a Python module called ‘paramiko’, which establishes an SSH session with the network devices and iterates over each device to obtain the show output. If that sounds a little bit confusing, no worries at all. All you have to do is replace the list ‘routers’ with your device hostnames and the username and password in the script. That’s it! Running the program will give you outputs of all the devices in the list one below the other. It doesn’t matter if you have 10 devices or 1000. You just have to run the script once. Once the script is run, Python will ask you to enter a command. At this point, enter the show command you want to use, like ‘show ip route’ or ‘show clock’.
import paramiko # this python module lets us connect to devices over ssh
ssh = paramiko.SSHClient() ''' Create a variable and call the module. Variable 'ssh' is now an object, which is an instance of
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) ''' we want to accept the RSA key of the devices we want to connect to from
this host. Unknown keys get added to the 'known hosts' file for that session '''
cmd=raw_input("Enter a command: ") # variable 'cmd' gets assigned the command which is entered after running the program
routers=['router1','router2','core_router1','border_router2'] ''' variable 'routers' is a list of devices from which you want to get the
show output. Replace this list with the hostnames of your network devices, provided you have a DNS mapping available; otherwise enter
IP addresses '''
for i in routers: # i is the iterator which iterates over the above list one by one
ssh.connect(i, port=22, username='admin', password='password', allow_agent=False,look_for_keys=False) ''' this establishes
connectivity with the devices. Replace the usename and password with the appropriate values '''
stdin,stdout,stderr = ssh.exec_command(cmd) ''' stdin,stdout,stderr are standard streams used to input data, output data and
write error messages respectively. '''
x=stdout.readlines() # we store the output from the devices to variable 'x'
print '**********************************************************' # just adds a line between outputs of various devices
print i + ' ' + cmd # prints the name of the device and the name of the command
for j in x: # iterates over the outputs from the devices in the above list
print j # prints those outputs
You can check out this code on my Github page as well at https://github.com/networkturf/Network-Scripts.git
Let’s admit that Networking has been relatively stagnant over the last 2 decades, while things like server and storage administration have seen monumental advancements. However, with the advent of things like SDN, APIs, config management tools, etc. there been a meteoric rise in Network Automation in the past few years . Let’s hope that these advancements continue, along with our rate of innovation, so Networking can catch up with other technologies soon!