Mastering Python Networking

Book description

Become an expert in implementing advanced, network-related tasks with Python.

Key Features

  • Build the skills to perform all networking tasks using Python with ease
  • Use Python for network device automation, DevOps, and software-defined networking
  • Get practical guidance to networking with Python

Book Description

This book begins with a review of the TCP/ IP protocol suite and a refresher of the core elements of the Python language. Next, you will start using Python and supported libraries to automate network tasks from the current major network vendors. We will look at automating traditional network devices based on the command-line interface, as well as newer devices with API support, with hands-on labs. We will then learn the concepts and practical use cases of the Ansible framework in order to achieve your network goals.

We will then move on to using Python for DevOps, starting with using open source tools to test, secure, and analyze your network. Then, we will focus on network monitoring and visualization. We will learn how to retrieve network information using a polling mechanism, ?ow-based monitoring, and visualizing the data programmatically. Next, we will learn how to use the Python framework to build your own customized network web services.

In the last module, you will use Python for SDN, where you will use a Python-based controller with OpenFlow in a hands-on lab to learn its concepts and applications. We will compare and contrast OpenFlow, OpenStack, OpenDaylight, and NFV. Finally, you will use everything you’ve learned in the book to construct a migration plan to go from a legacy to a scalable SDN-based network.

What you will learn

  • Review all the fundamentals of Python and the TCP/IP suite
  • Use Python to execute commands when the device does not support the API or programmatic interaction with the device
  • Implement automation techniques by integrating Python with Cisco, Juniper, and Arista eAPI
  • Integrate Ansible using Python to control Cisco, Juniper, and Arista networks
  • Achieve network security with Python
  • Build Flask-based web-service APIs with Python
  • Construct a Python-based migration plan from a legacy to scalable SDN-based network

Who this book is for

If you are a network engineer or a programmer who wants to use Python for networking, then this book is for you. A basic familiarity with networking-related concepts such as TCP/IP and a familiarity with Python programming will be useful.

Table of contents

  1. Preface
    1. What this book covers
    2. What you need for this book
    3. Who this book is for
    4. Conventions
    5. Reader feedback
    6. Customer support
      1. Downloading the example code
      2. Downloading the color images of this book
      3. Errata
      4. Piracy
      5. Questions
  2. Review of TCP/IP Protocol Suite and Python Language
    1. The internet overview
      1. Servers, hosts, and network components
      2. The rise of datacenter
        1. Enterprise datacenters
        2. Cloud datacenters
        3. Edge datacenters
    2. The OSI model
    3. Client server models
    4. Network protocol suites
      1. The Transmission Control Protocol (TCP)
        1. Functions and Characteristics of TCP
        2. TCP messages and data transfer
      2. User Datagram Protocol (UDP)
      3. The Internet Protocol (IP)
        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
      10. Summary
  3. Low-Level Network Device Interactions
    1. The challenges of CLI
    2. Constructing a virtual lab
      1. Cisco Virtual Internet Routing Lab (VIRL)
        1. VIRL tips
      2. Cisco DevNet and dCloud
      3. GNS3
    3. Python Pexpect Library
      1. Installation
      2. The Pexpect overview
      3. Our first Expect program
      4. More Pexpect features
      5. Pexpect and SSH
      6. Putting things together for Pexpect
    4. The Python Paramiko library
      1. Installating Paramiko
      2. The 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
  4. API and Intent-Driven Networking
    1. Infrastructure as the Python code
      1. Screen scraping versus API structured output
      2. 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. Cisco and YANG model
      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. The Arista eAPI management
        1. The eAPI preparation
        2. eAPI examples
      2. The Arista Pyeapi library
        1. The Pyeapi installation
        2. Pyeapi examples
    5. Vendor neutral libraries
    6. Summary
  5. The Python Automation Framework - Ansible Basics
    1. A quick Ansible example
      1. The control node installation
      2. Your first Ansible playbook
        1. The Public key authorization
        2. The inventory file
        3. Our first playbook
    2. The advantages of Ansible
      1. Agentless
      2. Idempotent
      3. Simple and extensible
      4. The vendor Support
    3. The Ansible architecture
      1. YAML
      2. Inventories
      3. Variables
      4. Templates with Jinja2
    4. Ansible networking modules
      1. Local connections and facts
      2. Provider arguments
    5. The Ansible Cisco example
    6. The Ansible Juniper example
    7. The Ansible Arista example
    8. Summary
  6. The Python Automation Framework - Ansible Advance Topics
    1. Ansible conditionals
      1. The when clause
      2. 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
  7. 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 regular expressions
    5. Other tools
      1. Private VLANs
      2. UFW with Python
    6. Summary
  8. Network Monitoring with Python - Part 1
    1. Lab setup
    2. SNMP
      1. Setup
      2. PySNMP
    3. Python 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. 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
  10. 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 s 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
  11. OpenFlow Basics
    1. Lab setup
    2. Introducing OpenFlow
      1. Basic operations
      2. OpenFlow 1.0 vs 1.3
    3. Mininet
    4. The Ryu controller with Python
      1. Open vSwitch commands
      2. The Ryu firewall application
    5. Layer 2 OpenFlow switch
      1. Planning your application
      2. Application components
    6. The POX controller
    7. Summary
  12. Advanced OpenFlow Topics
    1. Setup
    2. OpenFlow operations with Ryu
    3. Packet inspection
    4. Static router
      1. Mininet topology
      2. Ryu controller code
        1. Ryu flow installation
        2. Ryu packet generation
        3. Final result
    5. Router with API
      1. Ryu controller with API
      2. API usage examples
    6. BGP router with OpenFlow
      1. Lab router setup
      2. Python with the BGP speaker library
      3. Ryu BGP application
    7. Firewall with OpenFlow
    8. Summary
  13. OpenStack, OpenDaylight, and NFV
    1. OpenStack
      1. OpenStack overview
      2. Networking in OpenStack
      3. Trying out OpenStack
    2. OpenDaylight
      1. OpenDaylight programming overview
      2. OpenDaylight example
    3. Summary
  14. Hybrid SDN
    1. Preparing the network
      1. Familiarize yourself with the existing framework and tools
      2. Network standardization
      3. Create minimum viable products
      4. Relentlessly experiment
    2. Greenfield deployment
    3. Controller redundancy
      1. Multiple controller example
    4. BGP migration example
      1. Migration segmentation
      2. VIRL and Mininet setup
      3. Cisco device configuration
      4. Ryu BGP speaker
      5. Mininet and REST Router
      6. Result and verification
    5. More BGP example
      1. Examine the JSONRPC over WebSocket
    6. Monitoring integration
    7. Secure TLS connection
    8. Physical switch selection
      1. Lab OpenFlow switches
      2. Incumbent vendor switches
      3. Whitebox Switches
    9. Summary

Product information

  • Title: Mastering Python Networking
  • Author(s): Eric Chou
  • Release date: June 2017
  • Publisher(s): Packt Publishing
  • ISBN: 9781784397005