NAPALM or Network automation and programmability abstraction layer with multivendor support. is an open source python library that implements a set of functions to interact with network devices of different vendors using a unified API.
This Software also helps in installing configurations, Merging configurations, Rolling back configurations etc…
The library also have some very useful functions that can be used to get useful information from network devices.
Here is the GitHub link to [NAPALM](GitHub – napalm-automation/napalm: Network Automation and Programmability Abstraction Layer with Multivendor support). You can find all the documentation here. Now use the below commands to install it on ubuntu.
apt-get update
apt get install python -y
apt-get install build-essential libssl-dev libffi-dev -y
apt-get install python3-pip
pip install cryptography
pip install netmiko
pip3 install -U napalm
Once you install NAPALM. You can use it to retrieve information from the devices. We are also importing JSON so that the retrieved information is structured in a format that is easy to read.
get_interfaces() & get_interfaces_counters() will get you the interfaces configured on the switch & and the counters of those interfaces.
# Import Json so that the output can be printed in a readable format
import json
from napalm import get_network_driver
driver = get_network_driver('ios')
iosl2 = driver('192.168.0.17', 'kapil', 'kapil')
iosl2.open()
# Get facts, interfaces, and interface counters from the device. Napalm provides a unified API to interact with network dev>
ios_output = iosl2.get_facts()
print(json.dumps(ios_output, indent=4))
ios_output = iosl2.get_interfaces()
print(json.dumps(ios_output, sort_keys = True, indent=4))
#sort_keys=True sorts the keys in the output for better readability
ios_output = iosl2.get_interfaces_counters()
print(json.dumps(ios_output, sort_keys = True, indent=4))
Now let us write a program to get the MAC address table from the device using NAPALM. The program will give that output. get_mac_address_table() function in napalm can be used to get the mac address table from the device.
# Import Json so that the output can be printed in a readable format
import json
from napalm import get_network_driver
driver = get_network_driver('ios')
iosl2 = driver('192.168.0.17', 'kapil', 'kapil')
iosl2.open()
# Get the MAC address table from the device. Napalm provides a unified API to interact with network devices.
ios_output = iosl2.get_mac_address_table()
print(json.dumps(ios_output, indent=4))
There are several functions that can be used to get information from the devices. Full list of all the options that can be used can be found on this link.
Configuring Devices using NAPALM
NAPALM tries to provide a common interface and mechanism to push configuration to network devices independent of their operating system. This method is useful in combination with tools like Ansible.
Let us say we want to push some configuration lets say an access list to the switch. We need to create a separate file & then call it in the python program.
The actual access list is presented in a separate file called acl1.cfg
# the below establishes a connection to the IOS device using NAPALM
import json
from napalm import get_network_driver
driver = get_network_driver('ios')
iosl2 = driver('192.168.0.16', 'kapil', 'kapil')
iosl2.open()
# Configure the device with the access list. The file acl1.cfg contains the access list configuration commands.
print("Configuring access list on the device...")
iosl2.load_merge_candidate(filename='acl1.cfg')
iosl2.commit_config()
iosl2.close()
We can further improve on the script by adding a compare feature. Below script will check if there are any acl entry is missing on the device that is present on the file. It will compare and add that acl entry that is missing.
# The below establishes a connection to the IOS device using NAPALM
import json
from napalm import get_network_driver
driver = get_network_driver('ios')
iosl2 = driver('192.168.0.16', 'kapil', 'kapil')
iosl2.open()
print("Configuring the device...")
iosl2.load_merge_candidate(filename='acl1.cfg')
diff = iosl2.compare_config()
if len(diff) > 0:
print("Differences found in the configuration:")
print(diff)
iosl2.commit_config()
else:
print("No differences found in the configuration.")
iosl2.discard_config()
iosl2.close()
Now let us even further enhance this script by working with more than one file. One of the file will have configuration required for ACL and the other one will have the OSPF configuration. Also the script will be pushed to more than one device.
# This code connects to multiple network devices using NAPALM using a list of IP addresses.
import json
from napalm import get_network_driver
devicelist = ['192.168.0.16', '192.168.0.17']
for device in devicelist:
print ("connecting to " + str(device))
driver = get_network_driver('ios')
iosl2 = driver(device, 'kapil', 'kapil')
iosl2.open()
iosl2.load_merge_candidate(filename='acl1.cfg')
diff = iosl2.compare_config()
if len(diff) > 0:
print("Differences found in the acl configuration for device " + str(device) + ":")
print(diff)
iosl2.commit_config()
else:
print("No differences found in the acl configuration for device " + str(device) + ".")
iosl2.discard_config()
iosl2.load_merge_candidate(filename='ospf1.cfg')
diff = iosl2.compare_config()
if len(diff) > 0:
print("Differences found in the OSPF configuration for device " + str(device) + ":")
print(diff)
iosl2.commit_config()
else:
print("No differences found in the OSPF configuration for device " + str(device) + ".")
iosl2.discard_config()
iosl2.close()
print("Disconnected from " + str(device))