Book Image

Mastering Python Networking - Fourth Edition

By : Eric Chou
Book Image

Mastering Python Networking - Fourth Edition

By: Eric Chou

Overview of this book

Networks in your infrastructure set the foundation for how your application can be deployed, maintained, and serviced. Python is the ideal language for network engineers to explore tools that were previously available to systems engineers and application developers. In Mastering Python Networking, Fourth edition, you'll embark on a Python-based journey to transition from a traditional network engineer to a network developer ready for the next generation of networks. This new edition is completely revised and updated to work with the latest Python features and DevOps frameworks. In addition to new chapters on introducing Docker containers and Python 3 Async IO for network engineers, each chapter is updated with the latest libraries with working examples to ensure compatibility and understanding of the concepts. Starting with a basic overview of Python, the book teaches you how it can interact with both legacy and API-enabled network devices. You will learn to leverage high-level Python packages and frameworks to perform network automation tasks, monitoring, management, and enhanced network security, followed by AWS and Azure cloud networking. You will use Git for code management, GitLab for continuous integration, and Python-based testing tools to verify your network.
Table of Contents (19 chapters)
17
Other Books You May Enjoy
18
Index

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 programming 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. Carl Montanari created the Scrapli library to take advantage of the latest Python 3 asyncio concurrency features. In recent years, Kirk, Carl, David Barroso from the NAPALM project, and others teamed up to create the awesome Nornir framework to provide a pure Python network automation framework.

For the most part, the libraries are flexible enough to be used together or separately. For example, Ansible (covered in Chapter 4, The Python Automation Framework – Ansible) uses both Paramiko and Ansible-NAPALM as the underlying libraries 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 the command-line interface. It has proven to be ineffective in network management with even moderate-sized networks. This chapter will introduce Python libraries that can work with that limitation. First, let us discuss some of the challenges with the CLI in more detail.