LibCoder

Introduction to Python Network Automation: The First Journey

1C Agda Python
Introduction to Python Network Automation: The First Journey
Автор: Choi Brendan
Дата выхода: 2021
Издательство: Apress Media, LLC.
Количество страниц: 885
Размер файла: 11,9 МБ
Тип файла: PDF
Добавил: LibCoder
Оглавление
About the Author ....................................................................................................xixAbout the Technical Reviewer ................................................................................xxiAcknowledgments ................................................................................................xxiiiIntroduction ...........................................................................................................xxv■■Chapter 1: Introduction to Python Network Automation ....1Laying the Foundation ....1Exploring Your Skills and Prerequisites ....2General Competencies of Three Main IT Domain Group ....3Comparative Analysis of IT Engineers’ Responsibilities ....6Python and Network Automation Studies ....10Why Do People Want to Learn Python? ....10What Do I Need to Study Network Automation Using Python? ....12Hardware: Minimum Specifications for Laptop or PC ....13Software Requirements ....14Building a Network Automation Development Environment Using GNS3 ....16Downloading Supplementary Guides and Source Code ....20Summary ....21Storytime 1: Mega Speed Shoe Factory ....21■■Chapter 2: Learn Python Basics on Windows ....23“Hello, World!” and print( ) Function in Interactive Mode ....24Preparing for the Python Exercises ....27Understanding Data Types ....28Indentation and Code Blocks ....30Commenting ....31Python Naming Convention ....32Doing the Python Exercises ....33Variables and Strings ....34Exercise 2-1: Create Variables and Assign Various Data Types ....34Exercise 2-2: Create Variables and Use the print( ) Function to Print Output ....35Exercise 2-3: Use Abbreviated Variable Assignment ....36Exercise 2-4: Enter a String, Escape Character Backslash (\), and type( ) Function ....36Exercise 2-5: Determine If a Variable Is a Container, a Tag, or a Pointer ....38Exercise 2-6: Use String Indexing ....39Exercise 2-7: Understand Variable Assignment Errors ....39Exercise 2-8: Avoid “SyntaxError: EOL While Scanning String Literal” ....40Exercise 2-9: Avoid “NameError: name ‘variable_name’ is not defined” ....40Exercise 2-10: Avoid “SyntaxError: invalid syntax” ....41Exercise 2-11: Avoid “TypeError: ‘str’ object is not callable” ....41Exercise 2-12: Add Multiline Comments with Triple Quotation Marks ....43Exercise 2-13: Use \ as an Escape Character to Remove Special Character Meanings ....44Exercise 2-14: Enter (Inject) Values/Strings in a String Using %s ....44Printing, Concatenating, and Converting Strings ....45Exercise 2-15: Use the print( ) and len( ) Functions to Create a Simple Function ....45Exercise 2-16: Use the lower( ) and upper( ) String Methods ....46Exercise 2-17: Perform String Concatenation and Use the str( ) Method ....46Exercise 2-18: Learn to Change a String Using Curly Brackets and .format( ) ....47Exercise 2-19: Adjust the Text Position with Curly Brackets and .format( ) ....48Exercise 2-20: Adjust the Number of Decimal Places ....49Exercise 2-21: Ask for and Receive User Input with input( ) ....49Exercise 2-22: Change a Word or Characters in a String ....50Recap: Variables and Strings ....51Numbers and Arithmetic Operators ....52Exercise 2-23: Use Arithmetic Operators ....52Exercise 2-24: Understand Integers vs. Strings ....53Recap: Arithmetic Operators ....53Booleans and Relational Operators ....54Exercise 2-25: Use Booleans ....54Exercise 2-26: Use Relational Operators ....55Exercise 2-27: Use Boolean Expressions to Test True or False ....55Exercise 2-28: Use Logical (Membership) Operators ....56Exercise 2-29: Change the Order of Operation with ( ) ....56Control Statements: if, elif, and else ....56Exercise 2-30: Use if and else ....57Exercise 2-31: Use if, elif, and else ....57Exercise 2-32: Write Code with if, elif, and else ....58Recap: Boolean and Conditionals ....59Functions ....59Exercise 2-33: Defining a Function ....60Exercise 2-34: Assign Default Values to a Function ....60Exercise 2-35: Define Hello and Goodbye Functions ....61Exercise 2-36: Use the Odd or Even Function ....62Exercise 2-37: Nest a Function Within a Function ....62Recap: Functions ....63Lists ....63Exercise 2-38: Create a List and Index Items ....63Exercise 2-39: Use append, extend, and insert in a List ....64Slicing ....65Exercise 2-40: Slice a List ....65Exceptions and Error Handling ....66Exercise 2-41: Avoid a ValueError Error ....66Exercise 2-42: Handle Errors with try and except in a List ....66Exercise 2-43: Find an Index of an Item in a List with the Customized Exception ....68Practicing Lists ....69Exercise 2-44: Practice a List ....69Using for Loops and while Loops ....71Exercise 2-45: Use the for Loop’s upper( ) and capitalize( ) Methods ....71Exercise 2-46: Use the while Loop and len( ) Function ....71Sorting and Ranges ....72Exercise 2-47: Use sort( ) vs. sorted( ) in a List ....72Exercise 2-48: Link Two Lists ....72Exercise 2-49: Find the List Length Using the len( ) Function ....73Exercise 2-50: Use range( ) and the for Loop ....73Exercise 2-51: Use the String List for loop( ) and range( ) with Arguments ....74Recap: Lists and Loops ....75Tuples ....76Exercise 2-52: See Some Basic Tuple Examples ....76Exercise 2-53: Convert a Tuple to a List ....76Exercise 2-54: Determine Whether a Tuple Immutable ....77Exercise 2-55: Convert a Tuple to a List and a List to a Tuple ....78Exercise 2-56: Use a for Loop in a Tuple ....78Exercise 2-57: Assign Multiple Variables to a Tuple ....79Exercise 2-58: Create a Simple Tuple Function ....79Exercise 2-59: Use Tuples as List Elements ....80Recap: Tuples ....80Dictionaries ....81Exercise 2-60: Understand Dictionary Basics ....81Exercise 2-61: Avoid a Dictionary TypeError and Convert Two Lists to a Single Dictionary ....82Exercise 2-62: Use Keys to Print Values from a Dictionary ....82Exercise 2-63: Change a Dictionary Value ....83Exercise 2-64: Add a New Set of Keys and Values to a Dictionary ....83Exercise 2-65: Find the Number of Dictionary Elements ....84Exercise 2-66: Delete Dictionary Keys and Values ....84Exercise 2-67: Write a Python Script with a Dictionary ....84Exercise 2-68: Use a Dictionary for a Loop and Formatting ....85Recap: Dictionaries ....86Handling Files ....87Exercise 2-69: Read and Display Hosts File from Your PC ....87Exercise 2-70: Open and Close Hosts Files ....88Exercise 2-71: Create Code to Close a File in Two Ways ....89Exercise 2-72: Create a Text File, and Read, Write, and Print ....90Exercise 2-73: Use rstrip( ) or lstrip( ) to Remove Whitespace ....93Exercise 2-74: Python File Mode Exercise: Use r Mode ....94Exercise 2-75: Python File Mode Exercise: Use r+ Mode ....95Exercise 2-76: Python File Mode Exercise: Use a Mode ....96Exercise 2-77: Python File Mode Exercise: Use a+ Mode ....99Exercise 2-78: Python File Mode Exercise: Use w Mode ....99Exercise 2-79: Python File Mode Exercise: Use w+ Mode ....100Exercise 2-80: Python File Mode Exercise: Use x Mode ....101Exercise 2-81: Python File Mode Exercise: Use x Mode ....102Exercise 2-82: Open a Byte File in Python ....103Exercise 2-83: Handle Errors with try and except ....104Recap: Python file Handling Concept ....105Using Python Modules ....105Time Module ....106Exercise 2-84: Import the time Module ....106Sleep Method ....106Exercise 2-85: Use the time.sleep( ) Function ....106Exercise 2-86: Browse a Path Using the sys Module ....107Exercise 2-87: Add a New Filepath Using the sys Module ....108Exercise 2-88: Check Built-ins and sys.builtin_module ....109Exercise 2-89: Use a Simple import sys Module in a try and except Exercise ....110Exercise 2-90: Understand Lambdas by Making a Calculator ....111Recap: Module Concepts ....112Summary ....112■■Chapter 3: More Python Exercises ....113Getting Ready for the Exercises ....113Exercise 3-1: Concatenate a List and a Tuple into a Single List ....114Exercise 3-2: Use Python as a Calculator ....114Exercise 3-3: Do Some Basic String format( ) Exercises ....114Exercise 3-4: Ask for a Username ....115Exercise 3-5: Get a Username: Version 1 ....116Exercise 3-6: Get a Username: Version 2 ....117Exercise 3-7: Get a Username: Version 3 ....118Exercise 3-8: Add a Temporary Filepath, Import ex3_7.py as a Module, and Run the Script ....120Exercise 3-9: Use Commas to Add Spaces Between Strings ....120Exercise 3-10: Practice if; if and else; and if, elif, and else ....121Exercise 3-11: Practice for ~ in range with end=‘ ’ ....122Exercise 3-12: Practice for ~ in range ....123Exercise 3-13: Practice for line in ~ ....123Exercise 3-14: Use the split( ) Method ....124Exercise 3-15: Practice with lstrip( ), rstrip( ), strip( ), upper( ), lower( ), title( ), and capitalize( ) ....124Exercise 3-16: Create a file and read it four different ways ....125Exercise 3-17: Read and Output Files for a More Detailed Understanding ....126Exercise 3-18: Use the getpass( ) Module and User Input ....127Exercise 3-19: Understand the Difference Between Encoding and Decoding ....128Exercise 3-20: Handle CSV Files in Python with the csv Module ....128Exercise 3-21: Output a CSV File ....132Exercise 3-22: Find the Price of a Cisco ISR 4331 Router from a CSV File ....132Exercise 3-23: Calculate the Total Cost of the Router Purchases: No Module ....133Exercise 3-24: Calculate the Total Cost of the Router Purchases: Using the csv Module ....134Exercise 3-25: Convert dd-mmm-yy and Calculate the Difference in Days and Then in Years ....134Exploring the Python IDE Environment ....136Summary ....137Storytime 2: Machine vs. Human, The First Confrontation ....138■■Chapter 4: Introduction to VMware Workstation ....139VMware Workstation at a Glance ....139Type-1 vs. Type-2 Hypervisors ....141VMware Workstation Pro for a Proof-of-Concept Lab ....142Before Using VMware Workstation ....142What’s Next on VMware Workstation 15 Pro? ....145VMware Workstation 15 Pro User Console ....145Basic Operations of VMware Workstation 15 Pro ....146VMware Workstation Pro: Basic Operations ....147VMware Workstation Menu ....149Virtual Network Adapters ....152Virtual Network Editor Overview ....152Virtual Network Interface Description ....155Revealing Each Virtual Network Interface Types ....159Summary ....168■■Chapter 5: Creating an Ubuntu Server Virtual Machine ....169Downloading and Installing a Ubuntu Server 20 Image ....170Downloading the Ubuntu Server 20.04 LTS Image ....171Installing Ubuntu Server 20.04 LTS ....172Logging In to a New Ubuntu Server 20 via SSH ....196Customize Ubuntu Server ....202Ubuntu VM Customization 1: Enable Root User SSH Login on Ubuntu Server 20.04 ....203Ubuntu VM Customization 2: Install a Desktop GUI and Other Packages ....205Ubuntu VM Customization 3: Enable Root User GUI Access ....210Taking a Snapshot of a Virtual Machine ....214Cloning a Virtual Machine ....217Summary ....222■■Chapter 6: Creating a CentOS 8 Server Virtual Machine ....223Downloading and Installing a CentOS 8 Server Image ....223Downloading the CentOS 8 Server Image ....224Installing CentOS 8 Server ....226Logging In to a New CentOS 8 Server via SSH ....260Managing a Network Adapter on Linux ....263Creating a GNS3 VM by Importing an .ova File ....269Downloading and Installing the GNS3 VM from an .ova File ....270Summary ....274Storytime 3: The Origin of Hypervisors ....274■■Chapter 7: Linux Fundamentals ....275Why Learn Linux? ....276The Beginning of Linux ....278Understanding the Linux Environment ....279Understanding Linux Directories and File Formats ....280vi vs. nano ....281Introduction to vi ....282Introduction to nano ....293Linux Basic Administration ....306Changing the Hostname ....306Linux Basic File and Directory Commands ....309Linux File and Directory Exercises ....311Summary ....327■■Chapter 8: Linux Basic Administration ....329Information on Linux: Kernel and Distribution Version ....329Information on Linux: Use the netstat Command to Validate TCP/UDP Ports ....332Installing TFTP, FTP, SFTP, and NTP Servers ....339FTP Server Installation ....340Installing the SFTP Server ....346Installing the TFTP Server ....350Installing the NTP Server ....355Linux TCP/IP Troubleshooting Exercise ....359Summary ....364Storytime 4: Becoming a Savvy Linux Administrator, Perhaps Every ITEngineer’s Dream? ....364■■Chapter 9: Regular Expressions for Network Automation ....367Why Regex? ....368To re or Not to re ....369Studying Regular Expressions Using Python ....371Method 1: Using Notepad++ ....372Method 2: Using the Linux Shell ....374Regular Expression Breakdown: [A-Z]{3}\d{4}[/]\w+ ....375Method 3: Using the Internet to Study Regular Expressions ....375Regex Operation: The Basics ....376Character Class ([]) ....378Dot (.): Single Character Match ....379Asterisk (*): Repetition ....380Plus (+): Repetition ....380{m, n}: Repetition ....380Python’s re Module ....382Python re String Methods ....383Match Object Method ....387Compile Options ....388re.DOTALL (re.S) ....389re.IGNORECASE (re.I) ....389re.MULTILINE (re.M) ....390re.VERBOSE (re.X) ....391\: Confusing Backslash Character ....392Regular Expressions: A Little Revision Plus More ....393More Metacharacters ....393Lookahead and Lookbehind Assertions ....403Lookahead, Lookbehind, and Noncapturing group ....403Practice More Lookarounds ....405Lookaround Application Examples ....407sub Method: Substituting Strings ....409Substitute Strings Using sub ....410Using sub and \g to Swap Positions ....410Insert a Function in sub Method ....411Summary ....413■■Chapter 10: GNS3 Basics ....415GNS3 at a Glance ....415Installing GNS3 for the First Time ....417Downloading the GNS3 Installation File ....417GNS3 Installation and Setup ....418GNS3 Installation ....418GNS3 Setup Procedures ....418Getting Familiar with GNS3 ....424GNS3 Menus and GUI ....424Gracefully Shutting Down GNS3 and GNS3 VM ....426Starting GNS3 as an Administrator ....428Using GNS3 for the First Time: Cisco IOS and Windows Lab ....429Cisco IOS Software License and Downloading an Older Cisco IOS ....430Decompressing Cisco IOS for GNS3 Use ....431Installing Cisco IOS on the GNS3 VM ....442Creating an IOS Lab Topology on GNS3 and Connecting to the Internet ....454Installing the Microsoft Loopback Adapter ....462Accessing GNS3 Network Devices Using MS Loopback ....469Configuring the GNS3 IOS Router Using a Python Script from the WindowsHost PC ....478Cisco IOS and GNS3 Appliance Lab ....482Importing and Installing the GNS3 Linux Appliance Server ....482Manually Assigning IP Address to GNS3 Linux Appliance Server ....486Using the GNS3 Appliance Linux’s Python to Manage R1 ....487Summary ....489■■Chapter 11: Cisco IOS Labs ....491Cisco IOS and the Linux VM Lab ....491Creating a New GNS3 Project for the Linux VM Lab ....492Uploading and Downloading Files to the GNS3 IOS Router from Linux VMs(File Transfer Testing Lab) ....503Copying (Cloning) a GNS3 Project ....511Summary ....514■■Chapter 12: Building a Python Automation Lab Environment ....515Cisco CML-PERSONAL Software License Information and Software Downloads ....516Downloading the Cisco CML-PERSONAL IOSvL2 (Switch) Image ....516Downloading the Cisco CML-PERSONAL IOSv (Router) Image andstartup_config Files ....517Installing the Cisco CML-PERSONAL L2 Switch and CML-PERSONAL L3 on GNS3 ....518Installing the Cisco CML-PERSONAL L2 Switch on GNS3 ....518Quick Communication Test on CML L2 Switch Integration on GNS3 ....528Installing the Cisco CML-PERSONAL L3 Router on GNS3 ....529Quick Communication Test on CML L3 Router Integration on GNS3 ....539Building a CML-PERSONAL Lab Topology ....541Summary ....548■■Chapter 13: Python Network Automation Lab: Basic Telnet ....549Python Network Automation: Telnet Labs ....549Telnet Lab 1: Interactive Telnet Session to Cisco Devices on a Python Interpreter ....551Telnet Lab 2: Configure a Single Switch with a Python Telnet Template ....557Telnet Lab 3: Configure Random VLANs Using a for Loop ....562Telnet Lab 4: Configure Random VLANs Using a while Loop ....566Telnet Lab 5: Configure 100 VLANs Using the for ~ in range Loop Method ....569Telnet Lab 6: Add a Privilege 3 User on Multiple Devices Using IP Addressesfrom an External File ....573Telnet Lab 7: Taking Backups of running-config (or startup-config) toLocal Server Storage ....579Summary ....582■■Chapter 14: Python Network Automation Labs: SSH paramiko and netmiko ....583Python Network Automation Labs Using the paramiko and netmiko Libraries ....583Python SSH Labs: paramiko ....584paramiko Lab 1: Configure the Clock and Time Zone of All Devices Interactivelyin the Python Interpreter ....584paramiko Lab 2: Configuring an NTP Server on Cisco Devices WithoutUser Interaction (NTP Lab) ....592paramiko Lab 3: Create an Interactive paramiko SSH Script to Save RunningConfigurations to the TFTP Server ....598Python SSH Labs: netmiko ....605netmiko Lab 1: netmiko Uses a Dictionary for Device Information,Not a JSON Object ....606netmiko Lab 2: Develop a Simple Port Scanner Using a Socket Module and ThenDevelop a nemiko Disable Telnet Script ....614netmiko Lab 3: config compare ....621Summary ....628■■Chapter 15: Python Network Automation Labs: cron and SNMPv3 ....629Cron and SNMPv3 Labs ....629Cloning a GNS3 Project for the Next Lab ....630Quick-Start to the Linux Scheduler, cron ....637Ubuntu 20.04 LTS Task Scheduler: crontab ....637CentOS8.1 Task Scheduler: crond ....641Learn cron Job Definitions with Examples ....646Using Python to Run an SNMPv3 Query ....648Quick-Start Guide to SNMP ....649Learn to Use SNMPwalk on a Linux Server ....653Borrowing Some Example Python SNMP Code ....666Summary ....673■■Chapter 16: Python Network Automation Labs: Ansible, pyATS, Docker,and the Twilio API ....675Python Network Automation Development Labs ....675Quick-Start Guide to Ansible: virtualenv Lab 1 ....676Installing virtualenv and Getting Started with Ansible ....678Quick-Start Guide to pyATS (Genie): VirtualEnv Lab 2 ....685Sendmail Lab Using an Imported Docker Image ....700Docker Components and Account Registration ....701Docker Installation ....702Test-Driving Docker ....703Docker Sendmail Python Lab ....707Lab: Sendmail Email Notification script Development ....712CPU Utilization Monitoring Lab: Send an SMS Message Using Twilio ....717TWILIO Account Creation, Install Twilio Python Module and SMS Message Setup ....718CPU Utilization Monitoring Lab with an SMS Message ....723Summary ....732■■Chapter 17: Upgrading Multiple Cisco IOS XE Routers ....733Practical Python Network Automation Lab: IOS XE Upgrade Lab ....733Applying OOP Concepts to Your Network ....734Flow Control and Controlling User Input: UID, PWD, and Information Collector ....738Lab Preparation ....743CSR 1000v IOS XE Software and Download ....744Cisco CSR 1000v Installation on VMware Workstation ....745Discussion of How an IOS (IOS-XE/XR) Is Upgraded on Cisco Devices ....756Tasks Involved in a Cisco IOS Upgrade ....756Summary ....759■■Chapter 18: Python Network Automation Lab: Cisco IOS UpgradeMini Tools Development ....761Cisco IOS Upgrade Application Development ....761Part A: Pre-check Tools Development Connectivity Validation Tool ....762Collect User’s Login Credentials and User Input ....774Collect a New IOS Filename and MD5 Value from a CSV File ....778Check the MD5 Value of the New IOS on the Server ....788Check the Flash Size on Cisco Routers ....792Make Backups of running-config, the Interface Status, and the Routing Table ....796Part B: IOS Uploading and More Pre-check Tools Development ....799IOS Uploading Tool ....799Check the New IOS MD5 Value on the Cisco Device’s Flash ....804Options to Stop or Reload the Routers ....810Check the Reloading Device and Perform a Post-Reload Configuration Verification ....819Summary ....829■■Chapter 19: Python Network Automation Labs: Combining andCompleting the Cisco IOS Upgrade Application ....831Creating a Single Python File ....832Summary ....852Final Words ....853Index ....855

Описание

Ниже — практический обзор по теме «python».

Learn and implement network automation within the Enterprise network using Python 3. This introductory book will be your guide to building an integrated virtual networking lab to begin your Network Automation journey and master the basics of Python Network Automation.

You'll also develop essential skills such as Python scripting, regular expressions, Linux and Windows administration, VMware virtualization, and Cisco networking from the comfort of your laptop/PC with no actual networking hardware. The book features a review of the practical Python network automation scripting skills and tips learned from the production network, so you can safely test and practice in a lab environment first, various Python modules such as paramiko and netmiko, pandas, re, and much more. Finally, you will learn to write a fully automated and working Cisco IOS XE upgrade application using Python.

Introduction to Python Network Automation uses a canonical order, where you beginat the bottom and by the time you have completed this book, you will at least reach the intermediate level of Python coding for enterprise networking automation using native Python tools.

What You'll LearnBuild a proper GNS3-based networking lab for Python network automation needs

Write the basics of Python code in both the Windows and Linux environments

Control network devices using telnet, SSH, and SNMP protocols using Python codes

Understand virtualization and how to use VMware workstation

Examine virtualization and how to use VMware Workstation Pro

Develop a working Cisco IOS upgrade application

Who This Book Is ForIT engineers and developers, network managers and students, who would like to learn network automation using Python.

Файл доступен для загрузки ниже.

python network automation using networking book learn this

Частые вопросы

Можно ли скачать «Introduction to Python Network Automation: The First Journey» бесплатно?

Да, «Introduction to Python Network Automation: The First Journey» доступна для бесплатного скачивания на нашем сайте в формате PDF. Ссылка на файл находится на этой странице.

В каком формате и какого размера файл?

Книга предоставляется в формате PDF, размер файла 11,9 МБ.

Кто автор и когда вышла книга?

автор — Choi Brendan, издательство Apress Media, LLC., год выпуска 2021, 885 страниц.

О чём книга «Introduction to Python Network Automation: The First Journey»?

Learn and implement network automation within the Enterprise network using Python 3.

Похожие материалы