Mastering Python Networking
local graph
Mastering Python Networking
Section titled “Mastering Python Networking”chapter_01
Section titled “chapter_01”node-book_mastering-python-networking_chapter-01-review-of-tcp-ip-protocol-suite-and-python — Review of TCP/IP Protocol Suite and Python
chapter_02
Section titled “chapter_02”node-book_mastering-python-networking_chapter-02-low-level-network-device-interactions — Low-Level Network Device Interactions
chapter_03
Section titled “chapter_03”node-book_mastering-python-networking_chapter-03-apis-and-intent-driven-networking — APIs and Intent-Driven Networking
chapter_04
Section titled “chapter_04”node-book_mastering-python-networking_chapter-04-the-python-automation-framework-ansible — The Python Automation Framework – Ansible
chapter_05
Section titled “chapter_05”node-book_mastering-python-networking_chapter-05-docker-containers-for-network-engineers — Docker Containers for Network Engineers
chapter_06
Section titled “chapter_06”node-book_mastering-python-networking_chapter-06-network-security-with-python — Network Security with Python
chapter_07
Section titled “chapter_07”node-book_mastering-python-networking_chapter-07-network-monitoring-with-python-part-1 — Network Monitoring with Python – Part 1
chapter_08
Section titled “chapter_08”node-book_mastering-python-networking_chapter-08-network-monitoring-with-python-part-2 — Network Monitoring with Python – Part 2
chapter_09
Section titled “chapter_09”node-book_mastering-python-networking_chapter-09-building-network-web-services-with-python — Building Network Web Services with Python
chapter_10
Section titled “chapter_10”node-book_mastering-python-networking_chapter-10-introduction-to-async-io — Introduction to Async IO
chapter_11
Section titled “chapter_11”node-book_mastering-python-networking_chapter-11-aws-cloud-networking — AWS Cloud Networking
chapter_12
Section titled “chapter_12”node-book_mastering-python-networking_chapter-12-azure-cloud-networking — Azure Cloud Networking
chapter_13
Section titled “chapter_13”node-book_mastering-python-networking_chapter-13-network-data-analysis-with-elastic-stack — Network Data Analysis with Elastic Stack
chapter_14
Section titled “chapter_14”node-book_mastering-python-networking_chapter-14-working-with-git — Working with Git
chapter_15
Section titled “chapter_15”node-book_mastering-python-networking_chapter-15-continuous-integration-with-gitlab — Continuous Integration with GitLab
chapter_16
Section titled “chapter_16”node-book_mastering-python-networking_chapter-16-test-driven-development-for-networks — Test-Driven Development for Networks
Preface
Section titled “Preface”As Charles Dickens wrote in A Tale of Two Cities, “It was the best of times, it was the worst of times, it was the age of wisdom, it was the age of foolishness.” These seemingly contradictory words perfectly describe the chaos and mood felt during a time of change and transition. We are undoubtedly experiencing a similar time with the rapid changes in the network engineering field. As software development becomes more integrated into all engineering stacks, the traditional command-line interface and vertically integrated network control methods are no longer the best ways to manage today’s networks.
For network engineers, the changes we are seeing are full of excitement and opportunities and yet are challenging, particularly for those who need to quickly adapt and keep up. This book has been written to help ease the transition for networking professionals by providing a practical guide that addresses how to evolve from a traditional platform to one built on software-driven and development practices.
In this book, we use Python as the programming language of choice to master network engineering tasks. Python is an easy-to-learn, high-level programming language that can effectively complement network engineers’ creativity and problem-solving skills to streamline daily operations. Python is becoming an integral part of many large-scale networks, and through this book, I hope to share with you the lessons I’ve learned.
Since the publication of the first three editions of this book, I have been fortunate to have had interesting and meaningful conversations with many of the book’s readers. I am humbled by the success of the first three editions and took to heart the feedback I was given. In this fourth edition, I tried to incorporate many of the newer libraries, updated existing examples with the latest software and newer hardware platforms, added two new chapters, and significantly modified several chapters. I believe the changes are more reflective of today’s network engineering environment.
A time of change presents great opportunities for technological advancement. The concepts and tools in this book have helped me tremendously in my career, and I hope they can do the same for you.
Who this book is for
Section titled “Who this book is for”This book is ideal for IT professionals and operations engineers who already manage groups of network devices and would like to expand their knowledge of using Python and other tools to overcome network challenges. Basic knowledge of networking and Python is recommended.
What this book covers
Section titled “What this book covers”Chapter 1, Review of TCP/IP Protocol Suite and Python, reviews the fundamental technologies for internet communication today, from the OSI and client-server model to the TCP, UDP, and IP suites. The chapter will review the basics of the Python language, such as types, operators, loops, functions, and packages.
Chapter 2, Low-Level Network Device Interactions, uses practical examples to illustrate how to use Python to execute commands on a network device. It will also discuss the challenges of having a CLI-only interface in automation. The chapter will use the Pexpect, Paramiko, Netmiko, and Nornir libraries in the examples.
Chapter 3, APIs and Intent-Driven Networking, discusses the network devices that support Application Programming Interfaces (APIs) and other high-level interaction methods. It also illustrates tools that allow the abstraction of low-level tasks while focusing on the intent of the network engineers. A discussion about and examples of Cisco NX-API, Meraki, Juniper PyEZ, Arista Pyeapi, and Vyatta VyOS will appear in the chapter.
Chapter 4, The Python Automation Framework – Ansible, discusses the basics of Ansible. Ansible is an open source, Python-based automation framework. The framework moves one step further from APIs and focuses on declarative task intent. In this chapter, we will cover the advantages of using Ansible and its high-level architecture and see some practical examples of Ansible with network devices.
Chapter 5, Docker Containers for Network Engineers, explores containers and explains how Docker is the new standard in application development. In this chapter, we will introduce Docker as a tool by introducing the overall concepts and building example applications with it.
Chapter 6, Network Security with Python, introduces several Python tools to help you secure your network. It will discuss using Scapy for security testing, using Ansible to quickly implement access lists, and using Python for network forensic analysis.
Chapter 7, Network Monitoring with Python – Part 1, covers monitoring the network using various tools. The chapter contains some examples using SNMP and PySNMP for queries to obtain device information. Matplotlib and Pygal examples will be shown for graphing the results. The chapter will end with a Cacti example using a Python script as an input source.
Chapter 8, Network Monitoring with Python – Part 2, covers more network monitoring tools. The chapter will start with using Graphviz to graph the network from LLDP information. We will move to use examples with push-based network monitoring using NetFlow and other technologies. We will use Python to decode flow packets and ntop to visualize the results.
Chapter 9, Building Network Web Services with Python, shows you how to use the Python Flask web framework to create API endpoints for network automation. The network API offers benefits such as abstracting the requester from network details, consolidating and customizing operations, and providing better security by limiting the exposure of available operations.
Chapter 10, Introduction to Async IO, covers Async IO, the new Python 3 package that allows us to perform tasks at the same time. We will cover topics such as multiprocessing, parallelism, threading, and other concepts. We will also cover examples from the Scrapli project.
Chapter 11, AWS Cloud Networking, shows how we can use AWS to build a virtual network that is functional and resilient. We will cover virtual private cloud technologies such as CloudFormation, VPC routing tables, access lists, Elastic IP, NAT gateways, Direct Connect, and other related topics.
Chapter 12, Azure Cloud Networking, covers the network services by Azure and how to build network services with the service. We will discuss Azure VNet, Express Route and VPN, Azure network load balancers, and other related network services.
Chapter 13, Network Data Analysis with Elastic Stack, shows how we can use Elastic Stack as a set of tightly integrated tools to help us analyze and monitor our network. We will cover areas from installation, configuration, data import with Logstash and Beats, and searching data using Elasticsearch, to visualization with Kibana.
Chapter 14, Working with Git, illustrates how we can leverage Git for collaboration and code version control. Practical examples of using Git for network operations will be used in this chapter.
Chapter 15, Continuous Integration with GitLab, uses GitLab to automatically create operations pipelines that can save us time and increase reliability.
Chapter 16, Test-Driven Development for Networks, explains how to use Python’s unittest and pytest to create simple tests to verify our code. We will also see examples of writing tests for our network to verify reachability, network latency, security, and network transactions.
To get the most out of this book
Section titled “To get the most out of this book”To get the most out of this book, some basic hands-on network operation knowledge and Python knowledge is recommended. Most of the chapters can be read in any order, except for Chapter 4 and Chapter 5, which introduce base technologies that will be used later in the book. Besides the basic software and hardware tools introduced at the beginning of the book, new tools relevant to each of the chapters will be introduced in the respective chapters.
It is highly recommended to follow and practice the examples shown in your network lab.
Download the example code files
Section titled “Download the example code files”The code bundle for the book is hosted on GitHub at https://github.com/PacktPublishing/Mastering-Python-Networking-Fourth-Edition.
Conventions used
Section titled “Conventions used”There are a number of text conventions used throughout this book.
CodeInText: Indicates code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles. For example: “The auto-config also generated vty access for both Telnet and SSH.”
A block of code is set as follows:
# This is a commentprint("hello world")Any command-line input or output is written as follows:
$ python3Python 3.10.6 (main, Nov 2 2022, 18:53:38) [GCC 11.3.0] on linuxType "help", "copyright", "credits" or "license" for more information.Bold: Indicates a new term, an important word, or words that you see on the screen. For instance, words in menus or dialog boxes appear in the text like this. For example: “ In the coming section, we will continue with the SNMP theme of network monitoring but with a fully featured network monitoring system called Cacti.”
Warnings or important notes appear like this.
Tips and tricks appear like this.