O'Reilly logo

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Mastering Python Networking - Second Edition

Book Description

Master the art of using Python for a diverse range of network engineering tasks

Key Features

  • Explore the power of Python libraries to tackle difficult network problems efficiently and effectively
  • Use Python for network device automation, DevOps, and software-defined networking
  • Become an expert in implementing advanced network-related tasks with Python

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 this second edition of Mastering Python Networking, 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 book begins by reviewing the basics of Python and teaches you how Python can interact with both legacy and API-enabled network devices. As you make your way through the chapters, you will then learn to leverage high-level Python packages and frameworks to perform network engineering tasks for automation, monitoring, management, and enhanced security. In the concluding chapters, you will use Jenkins for continuous network integration as well as testing tools to verify your network.

By the end of this book, you will be able to perform all networking tasks with ease using Python.

What you will learn

  • Use Python libraries to interact with your network
  • Integrate Ansible 2.5 using Python to control Cisco, Juniper, and Arista eAPI network devices
  • Leverage existing frameworks to construct high-level APIs
  • Learn how to build virtual networks in the AWS Cloud
  • Understand how Jenkins can be used to automatically deploy changes in your network
  • Use PyTest and Unittest for Test-Driven Network Development

Who this book is for

Mastering Python Networking is for network engineers and programmers who want to use Python for networking. Basic familiarity with Python programming and networking-related concepts such as Transmission Control Protocol/Internet Protocol (TCP/IP) will be useful.

Downloading the example code for this book You can download the example code files for all Packt books you have purchased from your account at http://www.PacktPub.com. If you purchased this book elsewhere, you can visit http://www.PacktPub.com/support and register to have the files e-mailed directly to you.

Table of Contents

  1. Title Page
  2. Copyright and Credits
    1. Mastering Python Networking Second Edition
  3. Packt Upsell
    1. Why subscribe?
    2. PacktPub.com
  4. Contributors
    1. About the author
    2. About the reviewer
    3. Packt is searching for authors like you
  5. Preface
    1. Who this book is for
    2. What this book covers
    3. To get the most out of this book
      1. Download the example code files
      2. Download the color images
      3. Conventions used
    4. Get in touch
      1. Reviews
  6. 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. 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. User datagram protocol
      3. The internet protocol
        1. The IP NAT and 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
  7. Low-Level Network Device Interactions
    1. The challenges of the CLI
    2. Constructing a virtual lab
      1. Cisco VIRL
        1. VIRL tips
      2. Cisco DevNet and dCloud
      3. GNS3
    3. Python Pexpect library
      1. Pexpect installation
      2. Pexpect overview
      3. Our first Pexpect program
      4. More Pexpect features
      5. Pexpect and SSH
      6. Putting things together for Pexpect
    4. The Python Paramiko library
      1. Installation of Paramiko
      2. Paramiko overview
      3. Our first Paramiko program
      4. More Paramiko features
        1. Paramiko for servers
      5. Putting things together for Paramiko
    5. Looking ahead
      1. Downsides of Pexpect and Paramiko compared to other tools
        1. Idempotent network device interaction
        2. Bad automation speeds bad things up
    6. Summary
  8. APIs and Intent-Driven Networking
    1. Infrastructure as code
      1. Intent-Driven Networking
      2. Screen scraping versus API structured output
      3. Data modeling for infrastructure as code
    2. The Cisco API and ACI
      1. Cisco NX-API
        1. Lab software installation and device preparation
        2. NX-API examples
      2. The Cisco and YANG models
      3. The Cisco ACI
    3. 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
    4. The Arista Python API
      1. Arista eAPI management
        1. The eAPI preparation
        2. eAPI examples
      2. The Arista Pyeapi library
        1. Pyeapi installation
        2. Pyeapi examples
    5. Vendor-neutral libraries
    6. Summary
  9. The Python Automation Framework – Ansible Basics
    1. A more declarative framework
    2. A quick Ansible example
      1. The control node installation
      2. Running different versions of Ansible from source
      3. Lab setup
      4. Your first Ansible playbook
        1. The public key authorization
        2. The inventory file
        3. Our first playbook
    3. The advantages of Ansible
      1. Agentless
      2. Idempotent
      3. Simple and extensible
      4. Network vendor support
    4. The Ansible architecture
      1. YAML
      2. Inventories
      3. Variables
      4. Templates with Jinja2
    5. Ansible networking modules
      1. Local connections and facts
      2. Provider arguments
    6. The Ansible Cisco example
      1. Ansible 2.5 connection example
    7. The Ansible Juniper example
    8. The Ansible Arista example
    9. Summary
  10. The Python Automation Framework – Beyond Basics
    1. Ansible conditionals
      1. The when clause
      2. Ansible network facts
      3. Network module conditional
    2. Ansible loops
      1. Standard loops
      2. Looping over dictionaries
    3. Templates
      1. The Jinja2 template
      2. Jinja2 loops
      3. The Jinja2 conditional
    4. Group and host variables
      1. Group variables
      2. Host variables
    5. The Ansible Vault
    6. The Ansible include and roles
      1. The Ansible include statement
      2. Ansible roles
    7. Writing your own custom module
      1. The first custom module
      2. The second custom module
    8. Summary
  11. Network Security with Python
    1. The lab setup
    2. Python Scapy
      1. Installing Scapy
      2. Interactive examples
      3. Sniffing
      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 RE module
    5. Other tools
      1. Private VLANs
      2. UFW with Python
      3. Further reading
    6. Summary
  12. 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
  13. 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. Final playbook
    2. Flow-based monitoring
      1. NetFlow parsing with Python
        1. Python socket and struct
      2. ntop traffic monitoring
        1. Python extension for ntop
      3. sFlow
        1. SFlowtool and sFlow-RT with Python
    3. Elasticsearch (ELK stack)
      1. Setting up a hosted ELK service
      2. The Logstash format
      3. Python helper script for Logstash formatting
    4. Summary
  14. Building Network Web Services with Python
    1. Comparing Python web frameworks
    2. Flask and lab setup
    3. Introduction to Flask
      1. The HTTPie client
      2. URL routing
      3. URL variables
      4. URL generation
      5. The jsonify return
    4. Network resource API
      1. Flask-SQLAlchemy
      2. Network content API
      3. Devices API
      4. The device ID API
    5. Network dynamic operations
      1. Asynchronous operations
    6. Security
    7. Additional resources
    8. Summary
  15. AWS Cloud Networking
    1. AWS setup
      1. 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 the network ACL
      4. Elastic IP
      5. NAT Gateway
    4. Direct Connect and VPN
      1. VPN Gateway
      2. Direct Connect
    5. Network scaling services
      1. Elastic Load Balancing
      2. Route53 DNS service
      3. CloudFront CDN services
    6. Other AWS network services
    7. Summary
  16. Working with Git
    1. Introduction to Git
      1. Benefits of Git
      2. Git terminology
      3. Git and GitHub
    2. Setting up Git
      1. Gitignore
    3. Git usage examples
      1. GitHub example
        1. Collaborating with pull requests
    4. Git with Python
      1. GitPython
      2. PyGitHub
    5. Automating configuration backup
    6. Collaborating with Git
    7. Summary
  17. Continuous Integration with Jenkins
    1. Traditional change-management process
    2. Introduction to continuous integration
    3. Installing Jenkins
    4. Jenkins example
      1. First job for the Python script
      2. Jenkins plugins
      3. Network continuous integration example
    5. Jenkins with Python
    6. Continuous integration for Networking
    7. Summary
  18. Test-Driven Development for Networks
    1. Test-driven development overview
      1. Test definitions
    2. Topology as code
      1. Python's unittest module
    3. More on Python testing
      1. pytest examples
    4. 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
    5. Pytest in Jenkins
      1. Jenkins integration
    6. Summary
  19. Other Books You May Enjoy
    1. Leave a review - let other readers know what you think