Simple Python for Network Engineers

If you are a network engineer in 2018, and still think that your career is safe being a Putty Bandit (AKA console monkey) for the foreseeable future, then you are sorely mistaken.  In fact, Python is the language (part scripting, part programming) that will be automating your job in the next 10 years.  So the time is now to get at least comfortable with the software-defined future.  Here’s a little something to get you started: simple python for network engineers.

Linux is Your Friend

I know this scares a lot of you network admins out there.  But it’s time to get out of your comfort zone and spin up a GNU/Linux image.  For your standard network admin, the best distribution for you is Ubuntu.  There’s a huge community of support out there.  So start there.  Get your feet wet, and jump in.  So on your Windows or Macbook, get Oracle Virtualbox installed.  Then you can install a whole other OS running Ubuntu as a virtual machine.  I’m not going to go through this whole process, there’s a bunch of helpful tutorials for installing Ubuntu on Virtualbox out there like Itsfoss and Lifewire.

Simple Python for Network Engineers

The first thing is to make sure PIP, Python, Netmiko, and Paramiko are installed with all of their dependencies.  So if you followed my advice on Ubuntu, go ahead and run the following to make sure you have all the correct libraries – open the terminal:

sudo apt-get install python-paramiko python python-pip
sudo pip install --upgrade pip
sudo pip install netmiko

Once you get everything installed, open your text editor (vi is my favorite) and copy/paste the below python script for a Cisco device.  Works best with Cisco IOS routers like switches or routers.  Now type:

vim cisco.py

then hit the button for the letter “i” then paste the text below – make sure you change the username “YOURUSERNAME” to the username you use on your devices or central AAA server.

#!/usr/bin/env python
import getpass
import csv
import netmiko
import paramiko
from argparse import ArgumentParser

def main():
parser = ArgumentParser(description='Arguments for running cisco.py')
parser.add_argument('-c', '--csv', required=True, action='store', help='Location of CSV file')
args = parser.parse_args()

# ssh_username = input("SSH username: ")
ssh_username = "YOURUSERNAME"
ssh_password = getpass.getpass('SSH Password: ')

with open(args.csv, "r") as file:
reader = csv.DictReader(file)
for device_row in reader:
try:
ssh_session = netmiko.ConnectHandler(device_type='cisco_ios', ip=device_row['device_ip'],
username=ssh_username, password=ssh_password)

print("+++++ {0} +++++".format(device_row['device_ip']))
ssh_session.send_command("terminal length 0")
print(ssh_session.send_command("sh ip int br"))
ssh_session.send_command("terminal length 30")
ssh_session.disconnect()

except (netmiko.ssh_exception.NetMikoTimeoutException,
netmiko.ssh_exception.NetMikoAuthenticationException,
paramiko.ssh_exception.SSHException) as s_error:
print(s_error)

if __name__ == "__main__":
main()

**to save and close in vi, hit the ESC button, then type

:wq

Now, using vi again create a file with a listing of the IPv4 addresses of your routers and switches like the below called “cisco.csv”

vim cisco.csv

then enter the below

device_ip
10.1.1.1
10.2.2.2
10.3.3.3

Close and save that file, then you can execute it like:

python cisco.py -c cisco.csv

You should get the output of the command “show ip interface brief” for all the devices organized in the output.  Here’s an example from my network:

python cisco.py -c cisco.csv
SSH Password:
+++++ 10.255.0.1 +++++
Interface IP-Address OK? Method Status Protocol
Embedded-Service-Engine0/0 unassigned YES NVRAM administratively down down
GigabitEthernet0/0 1.1.1.1 YES NVRAM up up
GigabitEthernet0/1 2.2.2.2 YES NVRAM up up
GigabitEthernet0/1.10 3.3.3.3 YES NVRAM up up
GigabitEthernet0/1.27 4.4.4.4 YES NVRAM up up
GigabitEthernet0/2 5.5.5.5.5 YES NVRAM up up
Loopback0 unassigned YES unset up up
Loopback1 7.7.7.7 YES NVRAM up up
Tunnel5 8.8.8.8 YES unset up up

Obviously I changed the IPs for security reasons.  However, you can run the same command for IPv6 as well – just change the command “sh ip int br” to “sh ipv6 int br”

See, Wasn’t that bad

In summary, you can use this as a baseline script to pull all kinds of info from your switches and routers.  Here’s a few things, but the possibilities are endless, and now you have some experience in network automation!

  • CPU utilization: “show proc cpu hist”
  • which interfaces are up and connected on a switch: “show int status | i connected”

Want more info, or want to see how Tachyon Dynamics can help your network automation needs with Python or other useful tools? Contact us here!