Book Image

Mastering Python Networking - Third Edition

By : Eric Chou
Book Image

Mastering Python Networking - Third 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, Third edition, you’ll embark on a Python-based journey to transition from traditional network engineers to network developers ready for the next-generation of networks. This new edition is completely revised and updated to work with Python 3. In addition to new chapters on network data analysis with ELK stack (Elasticsearch, Logstash, Kibana, and Beats) and Azure Cloud Networking, it includes updates on using newer libraries such as pyATS and Nornir, as well as Ansible 2.8. 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 Azure and AWS Cloud networking. Finally, you will use Jenkins for continuous integration as well as testing tools to verify your network.
Table of Contents (18 chapters)
16
Other Books You May Enjoy
17
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 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.