Network automation with Python, Paramiko, Netmiko and NAPALM – TechTarget

Posted: January 18, 2020 at 10:37 am

Python is the most common language for network automation, but it can't do the job easily all by itself. Fortunately, several motivated individuals have created libraries that handle a lot of the details of network automation with Python. These automation libraries enable users to more easily build automation systems.

Users starting with automation should first gain a basic understanding of the Python scripting language and data structures, such as lists, tuples, dictionaries and sets. They'll also want to learn the basics of the Jinja2 templating language and YAML (YAML Ain't Markup Language). Users get a powerful automation framework when they combine Python scripting, Jinja2 for templating, YAML for data representation and the automation libraries below.

Network automation with Python and automation libraries can enable simplified communication with network devices. In this article, we take a look at three network automation libraries: Paramiko, Netmiko and NAPALM, or Network Automation Programmability Abstraction Layer with Multivendor support. Each library builds on its predecessor to provide greater layers of abstraction that enable users to build more efficient automation systems.

Paramiko is a low-level Secure Shell (SSH) client library. We can use it to programmatically control connecting to a network device's command-line interface (CLI) over a secure SSH connection. With the library, users send commands a person would normally type and parse the results of each command's execution, also known as screen scraping.

The Python script below uses the Paramiko library to query a Cisco Catalyst 3560 router for its Address Resolution Protocol (ARP) table. It is the first step of a script to identify the switch port where a device is connected.

Lines one to five import the additional libraries we need and define the IP address of the router we're going to query. Lines seven to 14 create an SSH connection to the router and login using the name tester with the password foobar. A command is sent to disable output pagination. Lines 16 to 18 send the show arp command, and line 21 reads the command result and prints it. The connection is closed when the script ends.

Here is the result:

Paramiko provides a low-level SSH interface to network devices. Many parameters that vary according to device model are encoded in the script, including changes in command syntax for different OSes. In addition, we would have to handle all the details of gathering data, making configuration changes and verifying those changes. To make a configuration change, we would need to manage each step of the process -- enabling access, entering config mode, sending the changes and saving the changes. Fortunately, other frameworks, like Netmiko, do a lot of this work for us.

The Netmiko library helps users hide many details of common device communications functions. It uses Paramiko for the low-level SSH connectivity, but it provides greater abstraction of the communication with a variety of network device models. Netmiko supports a wide range of devices, and the Netmiko script is much shorter than the Paramiko script, as seen below.

Lines one to four import the necessary modules, and lines five to 10 then create a Python dictionary that contains the connection parameters for this device. When working with many devices, users will need additional code to iterate over the devices, which they can easily do with a list of dictionaries. The device connection is one line, and another line sends the command and prints the output.

The Netmiko output is shorter as well, having stripped the device prompts from around the command output:

Netmiko used the device_type definition to properly handle communications with the device, and we didn't have to do any of the device-specific work. While Netmiko enables users to issue device commands and get the responses, it doesn't automate configuration management. That's where we use NAPALM.

NAPALM is built on top of Netmiko and provides a mechanism for interacting with device configurations. It supports a smaller set of devices than Netmiko: Arista, Juniper and Cisco (IOS, IOS XR, NX-OS and NX-OS SSH). It also supports a large set of getter functions that retrieve basic device data. The following script uses the get_arp_table() function:

The setup with NAPALM is longer than with Netmiko because it needs more libraries and the login parameters must be specified differently, as in lines two to 13. The connection is established in lines 15 to 17. Finally, the ARP table information is retrieved.

NAPALM returns it in Python dictionary format, so we use a library function to prettyprint it:

The dictionary output can be used directly within Python. The other libraries provide the CLI textual output, which would have to be converted to a Python data structure to be useful for other purposes.

The real power of NAPALM is in its ability to work with configurations, including the following:

Several online courses and videos cover network automation with Python and automation frameworks. Nick Russo's courses are a good starting point. Ivan Pepelnjak also explores other technologies in his more advanced "Building Network Automations Solutions" and "Ansible for Networking Engineers" (with David Barroso) courses.

See original here:

Network automation with Python, Paramiko, Netmiko and NAPALM - TechTarget

Related Posts