Low-Level Network Device Interactions
In Chapter 1, Review of TCP/IP Protocol Suite and Python, we looked at the theories and specifications behind network communication protocols. We also took a quick tour of the Python language. In this chapter, we will start to dive deeper into the management of network devices using Python. In particular, we will examine the different ways in which we can use Python to programmatically communicate with legacy network routers and switches.
What do I mean by legacy network routers and switches? While it's hard to imagine any networking device coming out today without an application program interface (API) for programmatic communication, it is a known fact that many of the network devices deployed in previous years did not contain API interfaces. The intended method of management for those devices was through command line interfaces (CLIs) using terminal programs, which were originally developed with a human engineer in mind. The management relied on the engineer's interpretation of the data returned from the device for appropriate action. As one can imagine, as the number of network devices and the complexity of the network grew, it became increasingly difficult to manually manage them one by one.
Python has several great libraries and frameworks that can help with these tasks, such as Pexpect, Paramiko, Netmiko, NAPALM, and Nornir, amongst others. It is worth noting that there are several overlaps between these libraries in terms of code, dependencies, and the maintainers of the projects. For example, the Netmiko library was created by Kirk Byers in 2014 based on the Paramiko SSH library. In 2017, Kirk and others teamed up with David Barroso from the NAPALM project to create the Nornir framework to provide a pure Python network automation framework.
For the most part, the libraries can be used concurrently, for example, Ansible (covered in Chapter 4, The Python Automation Framework – Ansible Basics, and Chapter 5, The Python Automation Framework – Beyond Basics) uses both Paramiko and Ansible-NAPALM for its network modules.
With so many libraries in existence today, it's not possible to cover all of them in a reasonable number of pages. In this chapter, we will cover Pexpect first, then move on with examples from Paramiko. Once we understand the basics and operations of Paramiko, it is easy to branch out to other libraries such as Netmiko and NAPALM. In this chapter, we will take a look at the following topics:
- The challenges of the CLI
- Constructing a virtual lab
- The Python Pexpect library
- The Python Paramiko library
- Examples from other libraries
- The downsides of Pexpect and Paramiko
We have briefly discussed the shortfalls of managing network devices via command line interface. It has proven to be ineffective in network management with moderate size networks. This chapter will introduce Python libraries that can work with that limitation. First, let us discuss some of the challenges with CLI in more detail.