Getting Started with Python Network Libraries for Network Engineers – Part 2
In the first part of this series, we looked at Netmiko. In this blog, we’ll look at NAPALM, another library that is available to address these challenges is. This post takes a look at the basics of NAPALM, and how it can be used to interact with network devices with a focus on data collection.
The currently supported platforms of NAPALM are – IOS, EOS, NXOS, and IOS-XR. See the support matrix for more detailed information on platform support and back-end library.
NAPALM 101 – Demo
I will be using a Cisco DevNet Sandbox to demonstrate the basic setup and use of NAPALM in a network environment. The sandbox is free to use – there are shared and dedicated sandbox environments you can use to try out new technologies.
For this tutorial, I am connected via SSH to a VM that has SSH access to a Cisco IOS XR router. On the VM, install the NAPALM library.
pip install napalm
In the Python shell, you can directly connect to the router using a few lines of code. The driver is selected based on which networking device you are connecting to, in this instance “ios”. See the docs on supported devices.
>>> import napalm
>>>
>>> driver = napalm.get_network_driver("ios")
>>> device = driver(hostname="10.10.20.70", username="admin", password="admin", optional_args={"port": 2221},)
>>> device.open()
Getters
The power of NAPALM is built on the getters. Getters are Python methods that have been written to return structured data in a normalized format. Using the getters you can retrieve information from the networking device and programmatically interact with it with Python. Using the JSON python library, you can structure the return data to be more readable. Below is an example using the get_interfaces_ip() getter.
>>> import json
>>> output = device.get_interfaces_ip()
>>> print(json.dumps(output, indent=4)
{
"MgmtEth0/RP0/CPU0/0":{
"ipv4":{
"192.168.122.21":{
"prefix_length":24
}
},
"ipv6":{
}
},
"Wed":{
"ipv6":{
}
},
"GigabitEthernet0/0/0/4":{
"ipv6":{
}
},
"GigabitEthernet0/0/0/2":{
"ipv6":{
}
},
"GigabitEthernet0/0/0/3":{
"ipv6":{
}
},
"GigabitEthernet0/0/0/0":{
"ipv6":{
}
},
"GigabitEthernet0/0/0/1":{
"ipv6":{
}
}
}
After you are finished making changes or gathering information, don’t forget to close the connection.
>>> device.close()
There are many other useful getters, such as – get_bgp_neighbors
, get_arp
, ping
, and traceroute
. These have been built and improved upon with community support. Information on contributing to the NAPALM library can be found here.
Additional Functionality
In addition to show command functionality, there is also support for configuration changes on network devices. For most supported platforms, there are methods to merge or replace config, and to compare changes before you commit. See the support matrix for platform support information.
Extending Support
The final item I’d like to touch on is the extensibility of NAPALM. If there is a method that exists but does not return data in the structure you need, you can extend the driver. Extending a NAPALM driver allows you to write custom methods in Python to enhance your structured data response.
Outside of the main NAPALM library, there is community support for additional drivers, such as the NAPALM PANOS driver in the NAPALM community GitHub.
Conclusion
NAPALM is a robust tool for network automation, and benefits from active open-source contributions. Take a look at the GitHub for additional information on support, contributing, and to see what’s happening in the community.
-Susan
New to Python libraries? NTC’s Training Academy is holding a 3-day course Automating Networks with Python I on September 26-28, 2022 with 50% labs to get you up to speed.
Visit our 2022 public course schedule to see our full list.
Tags :
Contact Us to Learn More
Share details about yourself & someone from our team will reach out to you ASAP!