Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletter Hub
Free Learning
Arrow right icon
timer SALE ENDS IN
0 Days
:
00 Hours
:
00 Minutes
:
00 Seconds
Practical Network Automation
Practical Network Automation

Practical Network Automation: A beginner's guide to automating and optimizing networks using Python, Ansible, and more , Second Edition

eBook
€17.99 €26.99
Paperback
€32.99
Subscription
Free Trial
Renews at €18.99p/m

What do you get with a Packt Subscription?

Free for first 7 days. $19.99 p/m after that. Cancel any time!
Product feature icon Unlimited ad-free access to the largest independent learning library in tech. Access this title and thousands more!
Product feature icon 50+ new titles added per month, including many first-to-market concepts and exclusive early access to books as they are being written.
Product feature icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Product feature icon Thousands of reference materials covering every tech concept you need to stay up to date.
Subscribe now
View plans & pricing
Table of content icon View table of contents Preview book icon Preview Book

Practical Network Automation

Python Automation for Network Engineers

This chapter will introduce ways to interact with network devices using Python. We will also see certain use cases on configuring network devices using configuration templates, and also write a modular code to ensure high reusability of the code to perform repetitive tasks. We will also see the benefits of parallel processing of tasks and the efficiency that can be gained through multithreading.

The following topics will be covered in this chapter:

  • Interaction of network devices
  • Network device configuration using template
  • Multithreading
  • Use cases

Technical requirements

Interacting with network devices

Python is widely used to perform multiple tasks. One of the tasks that widely uses Python is network automation. With its wide set of libraries (such as Netmiko and Paramiko), there are endless possibilities for network device interactions for different vendors. Owing to the support of Python, the list of supported devices continues to expand with the developer community, adding support for additional vendors as they are introduced to market.

Before we proceed to interact with devices, let us understand one of the most widely used libraries for network interactions. We will be using Netmiko to perform our network interactions.

Netmiko (https://github.com/ktbyers/netmiko) is a library/module in Python that is used extensively to interact with network devices. This is a multi-vendor library with support for Cisco IOS, NX-OS, firewalls, and many other...

Network device configuration using template

With all the routers reachable and accessible through SSH, let us configure a base template that sends the Syslog to a Syslog server and additionally ensures that only information logs are sent to the Syslog server. Also, after configuration, a validation needs to be performed to ensure that logs are being sent to the Syslog server.

The logging server info is as follows:

  • Logging server IP: 192.168.20.5
  • Logging port: 514
  • Logging protocol: TCP

Additionally, a loopback interface (loopback 30) needs to be configured with the {rtr} loopback interface description.

The code lines for the template are as follows:

logging host 192.168.20.5 transport tcp port 514
logging trap 6
interface loopback 30
description "{rtr} loopback interface"

To validate that the Syslog server is reachable, and that the logs sent are informational, use the...

Multithreading

A key focus area while performing operations on multiple devices is how quickly we can perform the actions. To put this into perspective, if each router takes around 10 seconds to log in, gather the output, and log out, and we have around 30 routers that we need to get this information from, we would need 10*30 = 300 seconds for the program to complete the execution. If we are looking for more advanced or complex calculations on each output, which might take up to a minute, then it will take 30 minutes for just 30 routers.

This starts becoming very inefficient when our complexity and scalability grows. To help with this, we need to add parallelism to our programs. What this simply means is that we log in simultaneously on all 30 routers, and perform the same task to fetch the output at the same time. Effectively, this means that we now get the output on all 30 routers...

Use cases

We will now review the topics discussed through some use cases that are being used as common scenarios. These use cases, along with helping us to understand the concepts, can also be leveraged or enhanced to create complex automation that can perform actions at very large scale.

Using regular expressions (regex)

Let us examine a basic use of regex by parsing a base configuration containing the interface configuration of a router.
The task is to identify the interfaces that have
trunk enabled:

import re
sampletext="""
interface fa0/1
switchport mode trunk
no shut

interface fa0/0
no shut

interface fa1/0
switchport mode trunk
no shut

interface fa2/0
shut

interface fa2/1
switchport mode trunk
no shut

interface te3...

Summary

In this chapter, we learned how to interact with Network devices through Python. We got familiar with an extensively used library of Python (Netmiko) for network interactions. Readers also got an insight into how to interact with multiple network devices using a simulated lab in GNS3 through examples on config generation and implementation through network templates.

Additionally, we also got to know the device interaction through SNMP, which is an industry standard method of interaction with various infrastructure devices. Using examples, we saw how to fetch types of devices as well as interface stats on each router in a quick and efficient manner.

Additionally, we also touched base on multithreading, which is a key component in scalability through various examples. To sum this aspect up, we saw certain use cases that are a real-life challenge for any network engineer...

Questions

  1. Does Netmiko support only Cisco-based devices? (Yes/No)

  2. Are the preferred hardware specifications for multithreading an entry-level machine or a high-end machine?

  3. Can we use SNMP to push any configuration on a Cisco router? (Yes/No)

  4. To push multiline configuration on a router through Netmiko, do we use a comma-separated string or a list?

  5. Efficiency-wise, is extracting the version of the Cisco router faster from Netmiko or SNMP?

  6. Does the network template needs to be hardcoded in a script when we want to push to a router? (Yes/ No)

  7. What is the term used for authenticating SNMP information from a router?

  8. Name of Python library/module used to create multiple threads.

  9. Name of Python library/module used to fetch information from network devices using SNMP.

Left arrow icon Right arrow icon
Download code icon Download Code

Key benefits

  • Get introduced to the concept of network automation with relevant use cases
  • Apply Continuous Integration and DevOps to improve your network performance
  • Implement effective automation using tools such as Python, Ansible, and more

Description

Network automation is the use of IT controls to supervise and carry out everyday network management functions. It plays a key role in network virtualization technologies and network functions. The book starts by providing an introduction to network automation, and its applications, which include integrating DevOps tools to automate the network efficiently. It then guides you through different network automation tasks and covers various data digging and performing tasks such as ensuring golden state configurations using templates, interface parsing. This book also focuses on Intelligent Operations using Artificial Intelligence and troubleshooting using chatbots and voice commands. The book then moves on to the use of Python and the management of SSH keys for machine-to-machine (M2M) communication, all followed by practical use cases. The book also covers the importance of Ansible for network automation, including best practices in automation; ways to test automated networks using tools such as Puppet, SaltStack, and Chef; and other important techniques. Through practical use-cases and examples, this book will acquaint you with the various aspects of network automation. It will give you the solid foundation you need to automate your own network without any hassle.

Who is this book for?

If you are a network engineer or a DevOps professional looking for an extensive guide to help you automate and manage your network efficiently, then this book is for you. No prior experience with network automation is required to get started, however you will need some exposure to Python programming to get the most out of this book.

What you will learn

  • Get started with the fundamental concepts of network automation
  • Perform intelligent data mining and remediation based on triggers
  • Understand how AIOps works in operations
  • Trigger automation through data factors
  • Improve your data center s robustness and security through data digging
  • Get access infrastructure through API Framework for chatbot and voice interactive troubleshootings
  • Set up communication with SSH-based devices using Netmiko

Product Details

Country selected
Publication date, Length, Edition, Language, ISBN-13
Publication date : Dec 26, 2018
Length: 226 pages
Edition : 2nd
Language : English
ISBN-13 : 9781789955651
Vendor :
Red Hat
Languages :
Tools :

What do you get with a Packt Subscription?

Free for first 7 days. $19.99 p/m after that. Cancel any time!
Product feature icon Unlimited ad-free access to the largest independent learning library in tech. Access this title and thousands more!
Product feature icon 50+ new titles added per month, including many first-to-market concepts and exclusive early access to books as they are being written.
Product feature icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Product feature icon Thousands of reference materials covering every tech concept you need to stay up to date.
Subscribe now
View plans & pricing

Product Details

Publication date : Dec 26, 2018
Length: 226 pages
Edition : 2nd
Language : English
ISBN-13 : 9781789955651
Vendor :
Red Hat
Languages :
Tools :

Packt Subscriptions

See our plans and pricing
Modal Close icon
€18.99 billed monthly
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Simple pricing, no contract
€189.99 billed annually
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just €5 each
Feature tick icon Exclusive print discounts
€264.99 billed in 18 months
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just €5 each
Feature tick icon Exclusive print discounts

Frequently bought together


Stars icon
Total 99.97
Python Automation Cookbook
€29.99
Hands-On RESTful Python Web Services
€36.99
Practical Network Automation
€32.99
Total 99.97 Stars icon

Table of Contents

8 Chapters
Fundamental Concepts of Network Automation Chevron down icon Chevron up icon
Python Automation for Network Engineers Chevron down icon Chevron up icon
Ansible and Network Templatizations Chevron down icon Chevron up icon
Using Artificial Intelligence in Operations Chevron down icon Chevron up icon
Web Framework for Automation Triggers Chevron down icon Chevron up icon
Continual Integration Chevron down icon Chevron up icon
Assessment Chevron down icon Chevron up icon
Other Books You May Enjoy Chevron down icon Chevron up icon

Customer reviews

Rating distribution
Full star icon Full star icon Full star icon Empty star icon Empty star icon 3
(2 Ratings)
5 star 0%
4 star 50%
3 star 0%
2 star 50%
1 star 0%
Paul Sep 05, 2021
Full star icon Full star icon Full star icon Full star icon Empty star icon 4
Would be 5 stars if it didn't have random stupid errors throughoutFor example p58, the code in the book is missing the function "puchconfig" and the only way to learn to code is to code not copy and paste. Found the missing function in the downloaded zip file..Good overall book though and would recommend..
Amazon Verified review Amazon
Fizzle Mar 05, 2022
Full star icon Full star icon Empty star icon Empty star icon Empty star icon 2
Got lost in the very first chapter because the author introduces Netmiko but does not show how to verify it and does not explain the order of commands and what we should expect at every turn. This is supposed to be a beginner`s guide? Why start by showing a bunch of use cases in different languanges. You did not even properly introduce those languages or how to acquire them and install them and know that the installation is good and then the first steps. Now I have to put down my new book and search Youtube were everyone is doing it with different hardware, and SW, and platforms. This book has powerful things but is an intermediate book.
Amazon Verified review Amazon
Get free access to Packt library with over 7500+ books and video courses for 7 days!
Start Free Trial

FAQs

What is included in a Packt subscription? Chevron down icon Chevron up icon

A subscription provides you with full access to view all Packt and licnesed content online, this includes exclusive access to Early Access titles. Depending on the tier chosen you can also earn credits and discounts to use for owning content

How can I cancel my subscription? Chevron down icon Chevron up icon

To cancel your subscription with us simply go to the account page - found in the top right of the page or at https://subscription.packtpub.com/my-account/subscription - From here you will see the ‘cancel subscription’ button in the grey box with your subscription information in.

What are credits? Chevron down icon Chevron up icon

Credits can be earned from reading 40 section of any title within the payment cycle - a month starting from the day of subscription payment. You also earn a Credit every month if you subscribe to our annual or 18 month plans. Credits can be used to buy books DRM free, the same way that you would pay for a book. Your credits can be found in the subscription homepage - subscription.packtpub.com - clicking on ‘the my’ library dropdown and selecting ‘credits’.

What happens if an Early Access Course is cancelled? Chevron down icon Chevron up icon

Projects are rarely cancelled, but sometimes it's unavoidable. If an Early Access course is cancelled or excessively delayed, you can exchange your purchase for another course. For further details, please contact us here.

Where can I send feedback about an Early Access title? Chevron down icon Chevron up icon

If you have any feedback about the product you're reading, or Early Access in general, then please fill out a contact form here and we'll make sure the feedback gets to the right team. 

Can I download the code files for Early Access titles? Chevron down icon Chevron up icon

We try to ensure that all books in Early Access have code available to use, download, and fork on GitHub. This helps us be more agile in the development of the book, and helps keep the often changing code base of new versions and new technologies as up to date as possible. Unfortunately, however, there will be rare cases when it is not possible for us to have downloadable code samples available until publication.

When we publish the book, the code files will also be available to download from the Packt website.

How accurate is the publication date? Chevron down icon Chevron up icon

The publication date is as accurate as we can be at any point in the project. Unfortunately, delays can happen. Often those delays are out of our control, such as changes to the technology code base or delays in the tech release. We do our best to give you an accurate estimate of the publication date at any given time, and as more chapters are delivered, the more accurate the delivery date will become.

How will I know when new chapters are ready? Chevron down icon Chevron up icon

We'll let you know every time there has been an update to a course that you've bought in Early Access. You'll get an email to let you know there has been a new chapter, or a change to a previous chapter. The new chapters are automatically added to your account, so you can also check back there any time you're ready and download or read them online.

I am a Packt subscriber, do I get Early Access? Chevron down icon Chevron up icon

Yes, all Early Access content is fully available through your subscription. You will need to have a paid for or active trial subscription in order to access all titles.

How is Early Access delivered? Chevron down icon Chevron up icon

Early Access is currently only available as a PDF or through our online reader. As we make changes or add new chapters, the files in your Packt account will be updated so you can download them again or view them online immediately.

How do I buy Early Access content? Chevron down icon Chevron up icon

Early Access is a way of us getting our content to you quicker, but the method of buying the Early Access course is still the same. Just find the course you want to buy, go through the check-out steps, and you’ll get a confirmation email from us with information and a link to the relevant Early Access courses.

What is Early Access? Chevron down icon Chevron up icon

Keeping up to date with the latest technology is difficult; new versions, new frameworks, new techniques. This feature gives you a head-start to our content, as it's being created. With Early Access you'll receive each chapter as it's written, and get regular updates throughout the product's development, as well as the final course as soon as it's ready.We created Early Access as a means of giving you the information you need, as soon as it's available. As we go through the process of developing a course, 99% of it can be ready but we can't publish until that last 1% falls in to place. Early Access helps to unlock the potential of our content early, to help you start your learning when you need it most. You not only get access to every chapter as it's delivered, edited, and updated, but you'll also get the finalized, DRM-free product to download in any format you want when it's published. As a member of Packt, you'll also be eligible for our exclusive offers, including a free course every day, and discounts on new and popular titles.