Time to burn through Multi-Vendor Environments using Python with NAPALM!
I’ve spun up a new network, as I believe I am going to wade into the waters of doing some router configuration using NAPALM scripting coming up in later posts!
Napalm is short for “Network Automation and Programmability Abstraction Layer with Multivendor Support” which is quite the mouthful, however the name is the recipe for NAPALM, more specifically the “Abstraction Layer” portion of it.
This “Abstraction Layer” creates of NAPALM creates a logical “layer” between the script and the specific vendor APIs and Protocols, allowing them to communicate through this abstraction, the specific Vendor Device / Configuration support Matrix can be found here:
One drawback to using Cisco IOS is it does NOT have a native API built into it that supports NAPALM via Python alone, however it will work with Python Netmiko scripting, which means to use NAPALM on Cisco IOS we must use Netmiko scripts.
This means that all lab devices will require SSH be enabled on them for Netmiko scripts!
The GitHub page for NAPALM can be found at the GitHub link below:
Though in my current lab I only need to issue the following commands on the NetAuto Host to get NAPALM installed and ready to use:
apt-get install python3-pip
pip3 install -U netmiko
pip3 install -U napalm
pip3 install -U simplejson
I make sure to run updates on anything I need to work with every time I begin to lab, as I’d hate to be chasing my tail troubleshooting an issue that is caused by not having the correct updated package installed, so I’d advise to perform this on the NetAuto Host.
A quick demonstration of what a NAPALM Python script looks like to start
Below is a very brief script to grab the config from SW1 in the new Topology:
With SSH enabled on SW and NetAuto Host all updated with Netmiko and NAPALM, the top 4 lines of this script are how we define the type of device (ios = cisco ios), along with the manual IP Address and Credentials to get logged in lines 1-4.
In this script I am using a simple function in line 6 that can be found on the Web site referenced above @ https://napalm.readthedocs.io/en/latest/support/ to get the command and ensure its compatible with IOS, and printing output on line 7 of the script as shown:
What these functions will pull from the device will of course depend on Vendor and their platform / output, however the fact that you can get it across so many different vendors is fantastic, with that I’ll quit talking and see what it grabs in my lab.
Lets give it a run on NetAuto Host and see what output it prints:
Note that this gives you what is either a Python List, or a JSON “Key Pair” meaning this value equals this output, so for example we see the following:
os_version : (ios), serial_number: (Serial #), fqdn: loopedback.com (configured on Switch), interfaces, vlans, uptime, etc.
We can then just change out the variable from the table into our script shown here:
Then save and run the script again to get the environment output for SW1:
Which WOULD give good information if this were not a virtual switch, but you get the idea, with those top 4 lines to define the device type / ip / login creds, you can then plug in any of the available commands in that matrix to pull IOS information and I am sure configure a few things as well!
I will end this lab with that quick demo and pick up deeper into scripting soon!
Just wanted to get the lab spun up, make sure I was able to execute a NAPALM script, and demonstrate not only myself but anyone reading this how powerful of a tool this as just 6 lines of code that probably would provide much more output with more configs.
I look forward to getting deeper into NAPALM, but more importantly into bed for the night, but I will be hitting Python as hard as possible with my DevNet Study Group coming up here in July – Have a great July 4th long weekend if I don’t write before it!