Mastering Python Networking - Fourth Edition

Book description

Get to grips with the latest container examples, Python 3 features, GitLab DevOps, network data analysis, and cloud networking to get the most out of Python for network engineering with the latest edition of this bestselling guide Purchase of the print or Kindle book includes a free eBook in PDF format.

Key Features

  • Explore the power of the latest Python libraries and frameworks to tackle common and complex network problems efficiently and effectively
  • Use Python and other open source tools for Network DevOps, automation, management, and monitoring
  • Use Python 3 to implement advanced network-related features

Book Description

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.

What you will learn

  • Use Python to interact with network devices
  • Understand Docker as a tool that you can use for the development and deployment
  • Use Python and various other tools to obtain information from the network
  • Learn how to use ELK for network data analysis
  • Utilize Flask and construct high-level API to interact with in-house applications
  • Discover the new AsyncIO feature and its concepts in Python 3
  • Explore test-driven development concepts and use PyTest to drive code test coverage
  • Understand how GitLab can be used with DevOps practices in networking

Who this book is for

Mastering Python Networking, Fourth edition is for network engineers, developers, and SREs who want to learn Python for network automation, programmability, monitoring, cloud, and data analysis. Network engineers who want to transition from manual to automation-based networks using the latest DevOps tools will also get a lot of useful information from this book. Basic familiarity with Python programming and networking-related concepts such as Transmission Control Protocol/Internet Protocol (TCP/IP) will be helpful in getting the most out of this book.

Table of contents

  1. Preface
    1. Who this book is for
    2. What this book covers
    3. To get the most out of this book
    4. Get in touch
    5. Share your thoughts
    6. Download a free PDF copy of this book
  2. Review of TCP/IP Protocol Suite and Python
    1. An overview of the Internet
      1. Servers, hosts, and network components
      2. The rise of data centers
        1. Enterprise data centers
        2. Cloud data centers
        3. Edge data centers
    2. The OSI model
    3. The client-server model
    4. Network protocol suites
      1. The transmission control protocol
        1. Functions and characteristics of TCP
        2. TCP messages and data transfer
      2. The user datagram protocol
      3. The Internet protocol
        1. IP network address translation (NAT) and network security
        2. IP routing concepts
    5. Python language overview
      1. Python versions
      2. Operating system
      3. Running a Python program
      4. Python built-in types
        1. The None type
        2. Numerics
        3. Sequences
        4. Mapping
        5. Sets
      5. Python operators
      6. Python control flow tools
      7. Python functions
      8. Python classes
      9. Python modules and packages
    6. Summary
  3. Low-Level Network Device Interactions
    1. The challenges of the CLI
    2. Constructing a Virtual lab
      1. Physical devices
      2. Virtual devices
      3. Cisco modeling labs
        1. CML tips
      4. Cisco DevNet
      5. GNS3 and others
    3. Python virtual environment
    4. Python pexpect library
      1. Pexpect installation
      2. Pexpect overview
      3. Our first Pexpect program
      4. More Pexpect Features
      5. Pexpect and SSH
      6. Pexpect complete example
    5. The Python Paramiko library
      1. Installation of Paramiko
      2. Paramiko overview
      3. First Paramiko program
      4. More Paramiko features
        1. Paramiko for servers
      5. More Paramiko examples
    6. The Netmiko library
    7. The Nornir framework
      1. Downsides of Pexpect and Paramiko compared to other tools
        1. Idempotent network device interaction
        2. Bad automation speeds up bad things
    8. Summary
  4. APIs and Intent-Driven Networking
    1. Infrastructure-as-Code (IaC)
      1. Intent-driven networking
      2. Screen scraping versus API structured output
      3. Data modeling for IaC
      4. YANG and NETCONF
    2. Cisco API examples
      1. Cisco NX-API
        1. Lab preparation
        2. NX-API examples
      2. The Cisco YANG Model
      3. The Cisco ACI examples
    3. Cisco Meraki controller
    4. The Python API for Juniper Networks
      1. Juniper and NETCONF
        1. Device preparation
        2. Juniper NETCONF examples
      2. Juniper PyEZ for developers
        1. Installation and preparation
        2. PyEZ examples
    5. The Arista Python API
      1. Arista eAPI management
        1. eAPI preparation
        2. eAPI examples
      2. The Arista Pyeapi library
        1. Pyeapi installation
        2. Pyeapi examples
    6. VyOS example
    7. Other libraries
    8. Summary
  5. The Python Automation Framework – Ansible
    1. Ansible – A More Declarative Framework
      1. Ansible Versions
    2. Our First Ansible Network Example
      1. The Control Node Installation
      2. Lab Topology
        1. The Variable Files
        2. Our First Playbook
    3. The Advantages of Ansible
      1. Agentless
      2. Idempotence
      3. Simple and Extensible
    4. Ansible Content Collections
    5. More Ansible Network Examples
      1. Inventory Nesting
      2. Ansible Conditionals
      3. Configuration Change
      4. Ansible Network Facts
      5. Ansible Loops
        1. Standard Loops
        2. Looping over Dictionaries
      6. Templates
        1. The Jinja Template Variables
        2. Jinja Template Loops
        3. Jinja Template Conditional
    6. Summary
  6. Docker Containers for Network Engineers
    1. Docker Overview
      1. Advantages of Docker
    2. Building Python applications in Docker
      1. Installing Docker
      2. Useful Docker commands
      3. Building hello world
      4. Building our application
      5. Sharing Docker images
      6. Container orchestration with Docker-compose
    3. Container networking
      1. Container host network
      2. Custom bridge network
      3. Other container network options
    4. Containers in the network engineering field
      1. Containerlab
    5. Docker and Kubernetes
    6. Summary
  7. Network Security with Python
    1. The Lab Setup
    2. Python Scapy
      1. Installing Scapy
      2. Interactive examples
      3. Packet captures with Scapy
      4. The TCP port scan
      5. The ping collection
      6. Common attacks
      7. Scapy resources
    3. Access lists
      1. Implementing access lists with Ansible
      2. MAC access lists
    4. The Syslog search
      1. Searching with the regular expression module
    5. Other tools
      1. Private VLANs
      2. UFW with Python
    6. Further reading
    7. Summary
  8. Network Monitoring with Python – Part 1
    1. Lab Setup
    2. SNMP
      1. Setup
      2. PySNMP
    3. Python for Data Visualization
      1. Matplotlib
        1. Installation
        2. Matplotlib – the first example
        3. Matplotlib for SNMP results
        4. Additional Matplotlib resources
      2. Pygal
        1. Installation
        2. Pygal – the first example
        3. Pygal for SNMP results
        4. Additional Pygal resources
    4. Python for Cacti
      1. Installation
      2. Python script as an input source
    5. Summary
  9. Network Monitoring with Python – Part 2
    1. Graphviz
      1. Lab setup
      2. Installation
      3. Graphviz examples
      4. Python with Graphviz examples
      5. LLDP neighbor graphing
        1. Information retrieval
        2. Python parser script
        3. Testing the playbook
    2. Flow-based monitoring
      1. NetFlow parsing with Python
        1. Python Socket and Struct
    3. ntop traffic monitoring
      1. Python extension for ntop
      2. sFlow
        1. SFlowtool and sFlow-RT with Python
    4. Summary
  10. Building Network Web Services with Python
    1. Comparing Python web frameworks
    2. Flask and lab setup
    3. Introduction to Flask
      1. Flask versions
      2. Flask examples
      3. The HTTPie client
      4. URL routing
      5. URL variables
      6. URL generation
      7. The jsonify return
    4. Network resource API
      1. Flask-SQLAlchemy
      2. The network content API
      3. The devices API
      4. The device ID API
    5. Network dynamic operations
      1. Asynchronous operations
    6. Authentication and Authorization
    7. Running Flask in containers
    8. Summary
  11. Introduction to Async IO
    1. Asynchronous operations overview
      1. Python multiprocessing
      2. Python multithreading
    2. Python asyncio module
    3. The Scrapli project
      1. Scrapli example
      2. Scrapli async example
    4. Summary
  12. AWS Cloud Networking
    1. AWS setup
      1. The AWS CLI and Python SDK
    2. AWS network overview
    3. Virtual Private Cloud
      1. Route tables and route targets
      2. Automation with CloudFormation
      3. Security Groups and Network ACLs
      4. Elastic IP
      5. NAT gateways
    4. Direct Connect and VPN
      1. VPN gateways
      2. Direct Connect
    5. Network scaling services
      1. Elastic Load Balancing
      2. Route 53 DNS service
      3. CloudFront CDN services
    6. Other AWS network services
    7. Summary
  13. Azure Cloud Networking
    1. Azure and AWS network service comparison
    2. Azure setup
    3. Azure administration and APIs
      1. Azure service principals
      2. Python versus PowerShell
    4. Azure global infrastructure
    5. Azure virtual networks
      1. Internet access
      2. Network resource creation
      3. VNet service endpoints
      4. VNet peering
    6. VNet routing
      1. Network security groups
    7. Azure VPNs
    8. Azure ExpressRoute
    9. Azure network load balancers
    10. Other Azure network services
    11. Summary
  14. Network Data Analysis with Elastic Stack
    1. What is the Elastic Stack?
    2. Lab topology
    3. Elastic Stack as a service
    4. First End-to-End example
    5. Elasticsearch with a Python client
    6. Data ingestion with Logstash
    7. Data ingestion with Beats
    8. Search with Elasticsearch
    9. Data visualization with Kibana
    10. Summary
  15. Working with Git
    1. Content management considerations and Git
    2. Introduction to Git
      1. Benefits of Git
      2. Git Terminology
      3. Git and GitHub
    3. Setting up Git
      1. Gitignore
    4. Git Usage Examples
    5. Git Branch
      1. GitHub Example
        1. Collaborating with Pull Requests
    6. Git with Python
      1. GitPython
      2. PyGitHub
    7. Automating Configuration Backup
    8. Collaborating with Git
    9. Summary
  16. Continuous Integration with GitLab
    1. The traditional change management process
    2. Introduction to continuous integration
    3. Installing GitLab
    4. GitLab runners
    5. First GitLab example
    6. GitLab network example
    7. Summary
  17. Test-Driven Development for Networks
    1. Test-driven development overview
      1. Test definitions
    2. Topology as code
      1. XML parsing example
      2. Python’s unittest module
      3. More on Python testing
      4. pytest examples
    3. Writing tests for networking
      1. Testing for reachability
      2. Testing for network latency
      3. Testing for security
      4. Testing for transactions
      5. Testing for network configuration
      6. Testing for Ansible
    4. pyATS and Genie
    5. Summary
  18. Other Books You May Enjoy
  19. Index

Product information

  • Title: Mastering Python Networking - Fourth Edition
  • Author(s): Eric Chou
  • Release date: January 2023
  • Publisher(s): Packt Publishing
  • ISBN: 9781803234618