Simple Python Script to Read from Device
There’s a lot of talk about network programmability and I recently had a simple use case that surfaced. The goal was locating a serial number in Cisco Devices. Basically, a script is required that will do the following.
- Process a list of IP Addresses and/or hostnames
- SSH into each device
- Determine if the device has a given SN
There are many ways this can be accomplished, but the method I am using utilizes SSH. This example requires the use of Paramiko to implement SSHv2. The script can match other items in the output of show version and can easily be modified to have multiple matches and return additional information.
Prerequisites
- Paramiko (can be installed using PIP)
- Python (tested with 2.7)
It is worth noting that the script I’m sharing will automatically add public ssh keys and therefore may not be appropriate in a high security environment.
The Python and sample device files can be downloaded here.
Python Code
import paramiko import getpass #get user/password/substring (for search) myuser = raw_input("Enter Username For Process: ") mypass = getpass.getpass() mysearch = raw_input("Please enter string to search: ") #get a list of devices from devices.txt - one per line qbfile = open("devices. Continue reading