- Your one-stop solution to using Python for network automation, DevOps, and Test-Driven Development -
Eric Chou
Book Details
Price
|
3.00 |
---|---|
Pages
| 454 p |
File Size
|
15,880 KB |
File Type
|
PDF format |
ISBN
| 978-1-78913-599-2 |
Copyright©
| 2018 Packt Publishing |
Eric Chou is a seasoned technologist with over 18 years of industry experience. He has worked on and helped managed some of the largest networks in the industry while working at Amazon AWS, Microsoft Azure, and other companies. Eric is passionate about network automation, Python, and helping companies build better security postures. Eric is the author of several books and online classes on networking with Python and network security. He is the proud inventor of two patents in IP telephony. Eric shares his deep interest in technology through his books, classes, and his blog, and contributes to some of the popular Python open source projects.
I would like to thank the open source and Python community members for generously sharing their knowledge and code with the public. Without their
contribution, many of the projects referenced in this book would not have been possible.
I would like to thank the Packt Publishing team for the opportunity to work on the second edition of the book, and the technical reviewer, Rickard Körkkö,
for generously agreeing to review the book.
To my wife and best friend, Joanna, I won the lottery the day I met you. To my two girls, Mikaelyn and Esmie, you make me so proud, I love you both dearly.
About the reviewer
Rickard Körkkö, CCNP (Routing and Switching) and Cisco Network Programmability Design and
Implementation Specialist, is a NetOps consultant at SDNit, where he's part of a group of experienced
technical specialists with a great interest in and focus on emerging network technologies. His daily work includes working with orchestration tools such as Ansible to manage network devices. He's a self-taught programmer with a primary focus on Python. He has also served as a technical reviewer for the book A Practical Guide to Linux Commands, Editors, and
Shell Programming, Third Edition by Mark G. Sobell.
Preface
As Charles Dickens wrote in A Tale of Two Cities, "It was the best of times, it was the worse of times, it was the age of wisdom, it was the age of foolishness." His seemingly contradictory words perfectly describe the chaos and mood felt during a time of change and transition. We are no doubt experiencing a similar time with the rapid changes in the fields of network engineering. As software development becomes more integrated into all aspects of networking, the traditional command-line interface and vertically integrated network stack 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 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 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 edition, I have been able to have interesting and meaningful conversations with many of the readers of the book. I am humbled by the success of the first edition of the book and took to the heart of the feedback I was given. In the second edition, I have tried to make the examples and technologies more relevant. In particular, the traditional OpenFlow SDN chapters were replaced with some of the Network DevOps tools. I sincerely hope the new addition is useful to you. 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.
Table of Contents
Title Page
Copyright and Credits
Mastering Python Networking Second Edition
Packt Upsell
Why subscribe?
PacktPub.com
Contributors
About the author
About the reviewer
Packt is searching for authors like you
Preface
Who this book is for
What this book covers
To get the most out of this book
Download the example code files
Download the color images
Conventions used
Get in touch
Reviews
1. Review of TCP/IP Protocol Suite and Python
An overview of the internet 
Servers, hosts, and network components
The rise of data centers
Enterprise data centers
Cloud data centers
Edge data centers
The OSI model
Client-server model
Network protocol suites
The transmission control protocol
Functions and characteristics of TCP
TCP messages and data transfer
User datagram protocol
The internet protocol
The IP NAT and security
IP routing concepts
Python language overview
Python versions
Operating system
Running a Python program
Python built-in types
The None type
Numerics
Sequences
Mapping
Sets
Python operators
Python control flow tools
Python functions
Python classes
Python modules and packages
Summary
2. Low-Level Network Device Interactions
The challenges of the CLI
Constructing a virtual lab
Cisco VIRL
VIRL tips
Cisco DevNet and dCloud
GNS3
Python Pexpect library
Pexpect installation
Pexpect overview
Our first Pexpect program
More Pexpect features
Pexpect and SSH
Putting things together for Pexpect
The Python Paramiko library
Installation of Paramiko
Paramiko overview
Our first Paramiko program
More Paramiko features
Paramiko for servers
Putting things together for Paramiko
Looking ahead
Downsides of Pexpect and Paramiko compared to other tools
Idempotent network device interaction
Bad automation speeds bad things up
Summary
3. APIs and Intent-Driven Networking
Infrastructure as code
Intent-Driven Networking
Screen scraping versus API structured output
Data modeling for infrastructure as code
The Cisco API and ACI
Cisco NX-API
Lab software installation and device preparation
NX-API examples
The Cisco and YANG models
The Cisco ACI
The Python API for Juniper networks
Juniper and NETCONF
Device preparation
Juniper NETCONF examples
Juniper PyEZ for developers
Installation and preparation
PyEZ examples
The Arista Python API
Arista eAPI management
The eAPI preparation
eAPI examples
The Arista Pyeapi library
Pyeapi installation
Pyeapi examples
Vendor-neutral libraries
Summary
4. The Python Automation Framework – Ansible Basics
A more declarative framework
A quick Ansible example
The control node installation
Running different versions of Ansible from source
Lab setup
Your first Ansible playbook
The public key authorization
The inventory file
Our first playbook
The advantages of Ansible
Agentless
Idempotent
Simple and extensible
Network vendor support
The Ansible architecture
YAML
Inventories
Variables
Templates with Jinja2
Ansible networking modules
Local connections and facts
Provider arguments
The Ansible Cisco example
Ansible 2.5 connection example
The Ansible Juniper example
The Ansible Arista example
Summary
5. The Python Automation Framework – Beyond Basics
Ansible conditionals
The when clause
Ansible network facts
Network module conditional
Ansible loops
Standard loops
Looping over dictionaries
Templates
The Jinja2 template
Jinja2 loops
The Jinja2 conditional
Group and host variables
Group variables
Host variables
The Ansible Vault
The Ansible include and roles
The Ansible include statement
Ansible roles
Writing your own custom module
The first custom module
The second custom module
Summary
6. Network Security with Python
The lab setup
Python Scapy
Installing Scapy
Interactive examples
Sniffing
The TCP port scan
The ping collection
Common attacks
Scapy resources
Access lists
Implementing access lists with Ansible
MAC access lists
The Syslog search
Searching with the RE module
Other tools
Private VLANs
UFW with Python
Further reading
Summary
7. Network Monitoring with Python – Part 1
Lab setup
SNMP
Setup
PySNMP
Python for data visualization
Matplotlib
Installation
Matplotlib – the first example
Matplotlib for SNMP results
Additional Matplotlib resources
Pygal
Installation
Pygal – the first example
Pygal for SNMP results
Additional Pygal resources
Python for Cacti
Installation
Python script as an input source
Summary
8. Network Monitoring with Python – Part 2
Graphviz
Lab setup
Installation
Graphviz examples
Python with Graphviz examples
LLDP neighbor graphing
Information retrieval
Python parser script
Final playbook
Flow-based monitoring
NetFlow parsing with Python
Python socket and struct
ntop traffic monitoring
Python extension for ntop
sFlow
SFlowtool and sFlow-RT with Python
Elasticsearch (ELK stack)
Setting up a hosted ELK service
The Logstash format
Python helper script for Logstash formatting
Summary
9. Building Network Web Services with Python
Comparing Python web frameworks
Flask and lab setup
Introduction to Flask
The HTTPie client
URL routing
URL variables
URL generation
The jsonify return
Network resource API
Flask-SQLAlchemy
Network content API
Devices API
The device ID API
Network dynamic operations
Asynchronous operations
Security
Additional resources
Summary
10. AWS Cloud Networking
AWS setup
AWS CLI and Python SDK
AWS network overview
Virtual private cloud
Route tables and route targets
Automation with CloudFormation
Security groups and the network ACL
Elastic IP
NAT Gateway
Direct Connect and VPN
VPN Gateway
Direct Connect
Network scaling services
Elastic Load Balancing
Route53 DNS service
CloudFront CDN services
Other AWS network services
Summary
11. Working with Git
Introduction to Git
Benefits of Git
Git terminology
Git and GitHub
Setting up Git
Gitignore
Git usage examples
GitHub example
Collaborating with pull requests
Git with Python
GitPython
PyGitHub
Automating configuration backup
Collaborating with Git
Summary
12. Continuous Integration with Jenkins
Traditional change-management process
Introduction to continuous integration
Installing Jenkins
Jenkins example
First job for the Python script
Jenkins plugins
Network continuous integration example
Jenkins with Python
Continuous integration for Networking
Summary
13. Test-Driven Development for Networks
Test-driven development overview
Test definitions
Topology as code
Python's unittest module
More on Python testing
pytest examples
Writing tests for networking
Testing for reachability
Testing for network latency
Testing for security
Testing for transactions
Testing for network configuration
Testing for Ansible
Pytest in Jenkins
Jenkins integration
Summary
Other Books You May Enjoy
Leave a review - let other readers know what you think
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 on using Python and other tools to overcome network challenges. Basic knowledge of networking and Python is recommended.