ebook img

Mastering Python Networking: Your one-stop solution to using Python for network automation, programmability, and DevOps PDF

577 Pages·2020·12.619 MB·English
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview Mastering Python Networking: Your one-stop solution to using Python for network automation, programmability, and DevOps

Mastering Python Networking Third Edition Your one-stop solution to using Python for network automation, programmability, and DevOps Eric Chou BIRMINGHAM - MUMBAI Mastering Python Networking Third Edition Copyright © 2020 Packt Publishing All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews. Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author, nor Packt Publishing or its dealers and distributors, will be held liable for any damages caused or alleged to have been caused directly or indirectly by this book. Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information. Producer: Tushar Gupta Acquisition Editor – Peer Reviews: Suresh Jain Project Editor: Tom Jacob Content Development Editor: Ian Hough Technical Editor: Karan Sonawane Copy Editor: Safis Editing Proofreader: Safis Editing Indexer: Manju Arasan Presentation Designer: Pranit Padwal First published: June 2017 Second edition: August 2018 Third edition: January 2020 Production reference: 1280120 Published by Packt Publishing Ltd. Livery Place 35 Livery Street Birmingham B3 2PB, UK. ISBN 978-1-83921-467-7 www.packt.com For my wife Joanna and children, Mikaelyn and Esmie. For my parents, who lit up the fire in me many years ago. packt.com Subscribe to our online digital library for full access to over 7,000 books and videos, as well as industry leading tools to help you plan your personal development and advance your career. For more information, please visit our website. Why subscribe? • Spend less time learning and more time coding with practical eBooks and Videos from over 4,000 industry professionals • Learn better with Skill Plans built especially for you • Get a free eBook or video every month • Fully searchable for easy access to vital information • Copy and paste, print, and bookmark content Did you know that Packt offers eBook versions of every book published, with PDF and ePub files available? You can upgrade to the eBook version at www.Packt.com and as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at [email protected] for more details. At www.Packt.com, you can also read a collection of free technical articles, sign up for a range of free newsletters, and receive exclusive discounts and offers on Packt books and eBooks. Foreword This book you are holding in your hand or are reading on your screen has a power that can be yours if you take time to study it. Much like Thor's hammer or Iron Man's suit, programming is a superpower that amplifies your existing knowledge and skill set. Many people feel, or are told, that they should learn programming and Python for their own sake. Programming skills are in demand, so you should be a programmer. That is probably good advice. But better advice would be to answer the question, "How can you take your existing expertise and leap ahead of peers by automating and extending that experience with software skills?" This book aims to do just that for network professionals. You'll learn Python in the context of network configuration, administration, monitoring, and more. If you are tired of logging in and typing a bunch of commands to configure your network, Python is for you. If you need to be certain that the network configuration is solid and repeatable, Python is for you. If you need to monitor, in real-time, what is happening on the network, well, you guessed it, Python is for you. You are probably in agreement about learning software skills that can be applied to network engineering. After all, terms like Software-Defined Networking (SDN) have been all the buzz in the last few years. But why Python? Maybe you should learn JavaScript or Go or some other language. Maybe you should just double down on Bash and shell scripting. Python is well-suited for network engineering for two reasons. First, as Eric will demonstrate throughout this book, there are many Python libraries (sometimes called packages) designed specifically on network engineering. A focused search over at https://pypi.org for the network topic turns up over 500 different libraries for network automation and monitoring. With libraries such as Ansible, you can create complex network and server configurations declaratively using simple configuration files. Using Pexpect or Paramiko, you'll be able to program against remote legacy systems as if they had their own scripting API. If the gear you're configuring has an API, chances are you can use a purpose-built Python library to work with it. So clearly, Python is well-suited for the job. Second, Python is special amongst programming languages. Python is what I call a full spectrum language. My definition of this term is that it is both a language that is incredibly easy to get started (print("hello world") anyone?) and also very powerful, being the technology behind incredible software such as youtube.com. This is not normal. We have solid beginner languages for quickly building software. Visual Basic comes to mind here. So does MATLAB and other commercial languages. Yet, when these are pushed too far, they fall down badly. Can you imagine Linux, Firefox, or an intensive video game created with any of these? No way. At the other end of the spectrum, we also have very powerful languages such as C++, .NET, Java, and many others. C++ is, in fact, the language used to build some Linux kernel modules and large open source software such as Firefox to some degree. Yet, these languages are not beginner friendly. You have to learn about pointers, compilers, linkers, headers, classes, accessibility (public/private), and so on just to get started. Python lives in both realms. On one hand it is incredibly easy to be productive with just a few lines of code and simple programming concepts. On the other, it is increasingly the language of choice for some of the world's most significant software; such as that behind YouTube, Instagram, Reddit, and others. Microsoft chose Python as their language to implement the command line interface (CLI) for Azure (although you don't have to know or use Python to use their CLI, of course). So, here's the deal. Programming is a superpower. It can take your network engineering expertise and launch it into the stratosphere. Python is one of the world's fastest growing and most popular programming language. Additionally, Python has many highly polished libraries for working with networks in many facets. This book, Mastering Python Networking, Third Edition, combines all of these and will change the way you think about networking. Enjoy the journey. Michael Kennedy Portland, OR Founder, Talk Python Introduction In 2014, I taught the first Coding 101 workshop on Python and REST APIs for network engineers in the DevNet Zone at Cisco Live. The room was full of prominent network engineers and architects, and many of them made their first API call in this workshop. Since then, I have been honored to work with network engineers from around the world who have decided to add coding to their skill set. IT and Operations teams are changing. I believe the new normal will be network engineers and software developers working side by side in the same team. The scalability, complexity, and security that modern application deployments demand from the network require automation to make network management repeatable, reliable, and agile at scale. Network engineers are problem solvers with deep expertise. Adding Python, network automation, and API skills to the network engineering tool set creates a powerful combination. With these added layers, engineers can approach problems in new ways and tackle new types of challenges. Mastering Python Networking, Third Edition is a valuable resource both for network engineers who want to learn coding skills and for software engineers who want to take advantage of new programmable infrastructure opportunities. One of the questions that I often hear from engineers is, "Where do I start?" My advice is: start simple. Look for challenges that your team faces, which are "read- only," and focus on using automation to troubleshoot and gather information. You can then use automation to flow the information that is gathered into ticketing systems or chat applications, and soon you have started to build a workflow. This safe-start, read-only evolution helps teams build confidence with automation and familiarity with the tools. At the beginning, focus on learning the core coding skills that will help you in every project including Python coding and RESTful APIs and on using tools like Git and GitHub to manage your source code and collaborate with others. Take the time to set up your development environment. Try different code editors and tools such as Postman and curl for exploring APIs. Build a solid understanding of how to work with JSON and XML. Start exploring software development methodologies such as test-driven development (TDD), and the core principles of DevOps. Mastering Python Networking, Third Edition is a great resource for working on these skills because it helps you learn these topics within the context of networking. The book starts with using Python for basic network device interactions using CLI and API, then moves up the stack with a general-purpose automation framework – all from the perspective of network engineers. Along the way, Eric provides Python examples on network security, monitoring, and constructing your own API with the Flask framework. Eric also introduces networking in the cloud with both AWS and Azure, as well as with common DevOps tools such as Git, Jenkins, and TDD. Throughout the book, Eric explains topics using a pragmatic, practitioner-based approach that helps you bring these concepts into your work. DevOps and Cloud are transforming our industry. Development, operations, security, and networking teams are connecting in new ways with shared goals and responsibilities to deliver business outcomes. Network engineers who learn software skills will help define this transformation. So, find your first project and work on learning these skills in the scope of that project. Pick something you do every day that you want to repeat reliably and try to automate it. Get hands on and write code early and often. Build or find a development lab where you can work. The more you can experiment, the faster you will learn. The innovations that are going to be created over the next five years by network engineers and software engineers working together are going to be game-changing. Take your first step, and don't forget to celebrate when you get your first 200 OK successful API response. Happy Coding! Mandy Whaley Senior Director of Developer Experience, Cisco DevNet Contributors About the author Eric Chou is a seasoned technologist with over 20 years of experience. He has worked on some of the largest networks in the industry while working at Amazon, Azure, and other Fortune 500 companies. Eric is passionate about network automation, Python, and helping companies build better security postures. In addition to being the author of Mastering Python Networking (Packt), he is also the co-author of Distributed Denial of Service (DDoS): Practical Detection and Defense (O'Reilly Media). Eric is also the primary inventor for two U.S. patents in IP telephony. He shares his deep interest in technology through his books, classes, and blog, and contributes to some of the popular Python open source projects. I would like to thank the open source, network engineering, and Python community members and developers for generously sharing their compassion, knowledge, and code. Without them, many of the projects referenced in this book would not have been possible. I hope I had made small contributions to these wonderful communities in my own ways as well. I would like to thank to the Packt team, Tushar, Tom, Ian, Alex, Jon, and many others, for the opportunity to collaborate on the third edition of the book. Special thanks to the technical reviewer, Rickard Körkkö, for generously agreeing to review the book. Thank you, Mandy and Michael for writing the Forewords for this book. I can't express my appreciation enough. You guys rock! To my parents and family, your constant support and encouragement made me who I am, I love you.

See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.