Network Programmability and Automation SKILLS FOR THE NEXT-GENERATION NETWORK ENGINEER Jason Edelman, Scott S. Lowe & Matt Oswalt Praise for Network Programmability and Automation Jason, Scott, and Matt have been key contributors in educating network engineers about both network automation and Linux networking. They have written and talked extensively about the importance of automation, on how automation impacts network engineers, and on the mechanics of automating networking devices. —Kirk Byers Creator of the Netmiko Python Library Network automation is no longer just a proof of concept: it represents both the present and the future! Network Programmability and Automation provides the needed background for modern engineers, by widening the toolset for more consistent, stable and reliable networks. —Mircea Ulinic Network Systems Engineer, Cloudflare Network automation is not hype anymore; it is a means to do your job faster, more consistently and more reliably. However, network automation is not just a single discipline; it is a collection of protocols, tools, and processes that can be overwhelming to the uninitiated. This book does a great job covering everything you will need to get your automation up and running. —David Barroso creator of NAPALM Network Programmability and Automation Skills for the Next-Generation Network Engineer Jason Edelman, Scott S. Lowe, and Matt Oswalt BBeeiijjiinngg BBoossttoonn FFaarrnnhhaamm SSeebbaassttooppooll TTookkyyoo Network Programmability and Automation by Jason Edelman, Scott S. Lowe, and Matt Oswalt Copyright © 2018 Jason Edelman, Scott S. Lowe, Matt Oswalt. All rights reserved. Printed in the United States of America. Published by O’Reilly Media, Inc., 1005 Gravenstein Highway North, Sebastopol, CA 95472. O’Reilly books may be purchased for educational, business, or sales promotional use. Online editions are also available for most titles (http://oreilly.com/safari). For more information, contact our corporate/insti‐ tutional sales department: 800-998-9938 or [email protected]. Editors: Virginia Wilson and Courtney Allen Interior Designer: David Futato Production Editor: Colleen Cole Cover Designer: Karen Montgomery Copyeditor: Dwight Ramsey Illustrator: Rebecca Demarest Proofreader: Rachel Monaghan Technical Reviewers: Patrick Ogenstad, Akhil Behl, Indexer: Judy McConville Eric Chou, Sreenivas Makam February 2018: First Edition Revision History for the First Edition 2018-02-02: First Release See http://oreilly.com/catalog/errata.csp?isbn=9781491931257 for release details. The O’Reilly logo is a registered trademark of O’Reilly Media, Inc. Network Programmability and Automa‐ tion, the cover image, and related trade dress are trademarks of O’Reilly Media, Inc. While the publisher and the authors have used good faith efforts to ensure that the information and instructions contained in this work are accurate, the publisher and the authors disclaim all responsibility for errors or omissions, including without limitation responsibility for damages resulting from the use of or reliance on this work. Use of the information and instructions contained in this work is at your own risk. If any code samples or other technology this work contains or describes is subject to open source licenses or the intellectual property rights of others, it is your responsibility to ensure that your use thereof complies with such licenses and/or rights. 978-1-491-93125-7 [M] I dedicate this book to all network engineers starting their network automation journey. I sincerely hope it provides each of you with the knowledge needed to further enhance your career. I’d also like to thank Scott, Matt, and the whole O’Reilly team—I know it was a much longer process than we all planned, but we ultimately got through it! Thanks to everyone for making it a reality. Jason Edelman I’d like to dedicate this book to the Lord, who granted me the wisdom and understanding I needed to write this book (Exodus 31:3 NIV). I’d also like to dedicate it to my wife, Crystal, without whose support things like this wouldn’t be possible. Scott S. Lowe I dedicate this book to anyone with a hunger and a passion for learning—every word was written with you in mind. I’d also like to thank my wife Jamie, who keeps me moti‐ vated and upbeat when life gets a little too crazy. Matt Oswalt Table of Contents Preface. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xiii 1. Network Industry Trends. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 The Rise of Software Defined Networking 1 OpenFlow 1 What Is Software Defined Networking? 5 Summary 16 2. Network Automation. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 Why Network Automation? 18 Simplified Architectures 18 Deterministic Outcomes 19 Business Agility 19 Types of Network Automation 20 Device Provisioning 20 Data Collection 23 Migrations 24 Configuration Management 25 Compliance 25 Reporting 26 Troubleshooting 26 Evolving the Management Plane from SNMP to Device APIs 28 Application Programming Interfaces (APIs) 28 Impact of Open Networking 32 Network Automation in the SDN Era 33 Summary 33 vii 3. Linux. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35 Examining Linux in a Network Automation Context 35 A Brief History of Linux 36 Linux Distributions 37 Red Hat Enterprise Linux, Fedora, and CentOS 37 Debian, Ubuntu, and Other Derivatives 39 Other Linux Distributions 40 Interacting with Linux 40 Navigating the Filesystem 41 Manipulating Files and Directories 46 Running Programs 52 Working with Daemons 55 Networking in Linux 60 Working with Interfaces 60 Routing as an End Host 71 Routing as a Router 75 Bridging (Switching) 77 Summary 83 4. Learning Python in a Network Context. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85 Should Network Engineers Learn to Code? 86 Using the Python Interactive Interpreter 88 Understanding Python Data Types 90 Learning to Use Strings 91 Learning to Use Numbers 100 Learning to Use Booleans 102 Learning to Use Python Lists 105 Learning to Use Python Dictionaries 111 Learning About Python Sets and Tuples 115 Adding Conditional Logic to Your Code 117 Understanding Containment 119 Using Loops in Python 121 Understanding the while Loop 121 Understanding the for Loop 122 Using Python Functions 126 Working with Files 129 Reading from a File 130 Writing to a File 132 Creating Python Programs 134 Creating a Basic Python Script 134 Understanding the Shebang 135 Migrating Code from the Python Interpreter to a Python Script 137 viii | Table of Contents