If you’re a networking passionate I’m pretty sure you’ve already heard about NAPALM (no, I’m not talking about the flammable liquid used in warfare 🙂 ). Anyway, if you’ve not yet, you’re going to discover a very nice project for network automation.
What is it? Let’s quote its documentation page:
NAPALM (Network Automation and Programmability Abstraction Layer with Multivendor support) is a Python library that implements a set of functions to interact with different network device Operating Systems using a unified API.
It’s a project developed by David Barroso and Elisa Jasinska (thank you guys 😀 ), owned by Spotify and, as the quote says itself, it is used to interact with different hardware networking vendors. Basically, it works like an API on top of other APIs, adding another level of abstraction.
This way, if I want to interact with a Juniper device I can use PyEZ, whereas I’d use NX-API if I wish to talk with a Nexus switch, and the example continues with other specialized APIs.
What NAPALM does is hiding this layer unifying the way we access a networking device, regardless who built it.
Back to few days ago, NAPALM supported the following network OS:
- Arista EOS
- Juniper JunOS
- Cisco IOS-XR
- Cisco NX-OS
- Fortinet FortiOS
- IBM OS
This is possible thanks to the introduction of the NetworkDriver concept. Every time we want to interact with a device, we can only specify what OS we are going to talk to and NAPALM will select the correct NetworkDriver (basically, a library with all the functions related to that OS).
>>> from napalm import get_network_driver >>> get_network_driver('eos') >>> >>> get_network_driver('iosxr') >>> >>> get_network_driver('junos') >>> >>> get_network_driver('fortios')
NAPALM will still use third party APIs but this will be trasparent to the user.
Cisco IOS support
Unlike NX-OS, Cisco IOS have no API support. Therefore, it’s not that straightforward to obtain structured data from it and at first NAPALM didn’t support it. So I thought this could be a nice spot to play 🙂
I forked the main repository and started to code.
Since no native API exists, I had to use something more general: netmiko. This is a pretty sweet Python library making it super easy to connect and interact with networking devices. Once a command is sent, netmiko can give me back the output and then I can start to filter and parse it.
The module is composed by 12 methods:
- open(): opens the connection with the remote device. It is the first method to be used.
- close(): closes the connection with the remote device.
- load_merge_candidate(filename, config): loads a candidate configuration from a textfile or a configuration string. If both are passed, filename is picked. At this point no configuration is pushed to the device yet.
- compare_config(): simply shows the list of commands proposed by the load_merge_candidate method and ready to be executed if commited.
- discard_config(): what if we notice some errors after the compare_config? We can discard the proposed changes using this method.
- commit_commit(): pushes the configuration from load_merge_candidate and saves the configuration.
- rollback(): we can rollback the commited changes using this method. This simply adds the no keyword to commands (anyway it’s smart enough to recognize parent/child commands)
- get_lldp_neighbors(): extracts lldp neighbors information from the device.
- get_facts(): extracts information like uptime, vendor, os_version, serial_number, model, hostname, fqdn, and interface_list.
- get_interfaces(): extracts information about interfaces including status and speed.
- get_bgp_neighbors(): extracts information about BGP neighbors.
- get_interfaces_counters(): extracts information about counters.
Now let’s see an example of how to use NAPALM.
The first thing we’re gonna do is to connect to our remote device specifying the OS type, username, password and IP address.
from netmiko import ConnectHandler from napalm import get_network_driver get_network_driver('ios') driver = get_network_driver('ios') device = driver('172.16.1.1', 'gabriele', 'gabriele') device.open()
Once we’re done, if everything went fine we’ll see the Python interactive shell confirming the SSH session has been established.
Now we can start to interact with our device. Let’s ask for some facts, for example:
As we can see, it’s a Cisco 3640 device whose IOS version is 12.4(16). It has 3 interfaces and its uptime value is set to 9 minutes.
Cool, right? 😀
Let’s try some other methods:
Earlier we discovered 3 interfaces exist in our device. Now we’ve just obtained some specific information about them using the get_interfaces() as well as BGP neighbors information thanks to get_bgp_neighbors().
Let’s see how NAPALM can help us with configuration management. Imagine we want to implement OSPF on our network. Just to keep it simple, we want to push the following configuration:
router ospf 1 ! network 0.0.0.0 0.0.0.0 area 1 !
Using load_merge_candidate(filename=new_good.conf), we’ll load our configuration (assuming new_good.conf is the textfile containing the above config). Then, we can see what changes would be implemented using compare_config(). At that point, we can decide to either commit or discard these changes.
Here we’ve sudden realized our OSPF area should be 0 and not 1. So we decide to discard the candidate configuration with discard_config(). We can use compare_config() to confirm that every possible change’s been discarded.
Since our last compare_config() doesn’t show anything, it means everything went fine.
Anyway, we still want to implement OSPF, so we fix the configuration and give it another try. This time we want to use a configuration string instead of a .conf file. We do this with load_merge_candidate(config=’router ospf 1\nnetwork 0.0.0.0 0.0.0.0 area 0′). Then, if we are happy with it, we can commit.
..and this is how the router’s config look like:
At this point, if we want to rollback the change we can simply use rollback().
It’s been lot of fun to work on this patch and I’m happy to announce that now NAPALM supports IOS too since my PullRequest has been merged to the main repo 🙂
NAPALM is a really cool project and it’s popular among NetOps community and it’s also been presented at a NANOG conference. Here you can find the video from the awesome guys who actually designed and implemented it. Enjoy 🙂
If you’re interested about NAPALM or Network Automation in general you should definitely join the SLACK channel at network.toCode(). Here you’ll find lots of cool guys discussing fancy stuff on networking 🙂