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
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Mastering Python Networking

You're reading from   Mastering Python Networking Your one-stop solution to using Python for network automation, programmability, and DevOps

Arrow left icon
Product type Paperback
Published in Jan 2020
Publisher Packt
ISBN-13 9781839214677
Length 576 pages
Edition 3rd Edition
Languages
Tools
Concepts
Arrow right icon
Author (1):
Arrow left icon
Eric Chou Eric Chou
Author Profile Icon Eric Chou
Eric Chou
Arrow right icon
View More author details
Toc

Table of Contents (18) Chapters Close

Preface 1. Review of TCP/IP Protocol Suite and Python 2. Low-Level Network Device Interactions FREE CHAPTER 3. APIs and Intent-Driven Networking 4. The Python Automation Framework – Ansible Basics 5. The Python Automation Framework – Beyond Basics 6. Network Security with Python 7. Network Monitoring with Python – Part 1 8. Network Monitoring with Python – Part 2 9. Building Network Web Services with Python 10. AWS Cloud Networking 11. Azure Cloud Networking 12. Network Data Analysis with Elastic Stack 13. Working with Git 14. Continuous Integration with Jenkins 15. Test-Driven Development for Networks 16. Other Books You May Enjoy
17. Index

The Netmiko library

Paramiko is a great library to do low-level interactions with Cisco IOS and other vendor devices. But if you have noticed from previous examples, we are repeating many of the same steps between iosv-1 and isov-2 for device login and execution. Once we start to develop more automation commands, we also start to repeat ourselves to capture outputs and format them into a usable format. Wouldn't it be great if somebody could write a Python library that simplifies these low-level steps and share it with other network engineers?

Ever since 2014, Kirk Byers (https://github.com/ktbyers) has been working on open source initiatives to simplify the management of network devices. In this section, we will take a look at an example of the Netmiko (https://github.com/ktbyers/netmiko) library that he created.

First, we will install the netmiko library using pip:

(venv) $ pip install netmiko

We can use the example published on Kirk's website, https://pynet.twb-tech.com/blog/automation/netmiko.html, and apply it to our labs. We will start by importing the library and its ConnectHandler class. Then we will define our device parameter as a Python dictionary and pass it to the ConnectHandler. Notice that we are defining a device_type of cisco_ios in the device parameter.

>>> from netmiko import ConnectHandler
>>> ios_v1 = {'device_type': 'cisco_ios', 'host': '172.16.1.20', 'username': 'cisco', 'password': 'cisco'}
>>> net_connect = ConnectHandler(**ios_v1)

This is where the simplification begins. Notice that the library automatically determines the device prompt as well as formatting the returned output from the show command:

>>> net_connect.find_prompt()
'iosv-1#'
>>> output = net_connect.send_command('show ip int brief')
>>> print(output)
Interface                  IP-Address      OK? Method Status                Protocol
GigabitEthernet0/0         172.16.1.20     YES NVRAM  up                    up      
GigabitEthernet0/1         10.0.0.5        YES NVRAM  up                    up      
Loopback0                  192.168.0.1     YES NVRAM  up                    up      

Let's see another example for the second Cisco IOS device in our lab, but this time we will define the iosv-2 parameter when we initiate the ConnectHandler object and send a configuration command instead of a show command. Note that the command attribute is a list that can contain multiple commands:

>>> net_connect_2 = ConnectHandler(device_type='cisco_ios', host='172.16.1.21', username='cisco', password='cisco')
>>> output = net_connect_2.send_config_set(['logging buffered 19999'])
>>> print(output)
config term
Enter configuration commands, one per line.  End with CNTL/Z.
iosv-2(config)#logging buffered 19999
iosv-2(config)#end
iosv-2#
>>> exit()

The netmiko library is a great time saver and is used by many network engineers. In the next section, we will take a look at the Nornir (https://github.com/nornir-automation/nornir) framework, which aims to simplify low-level interactions.

You have been reading a chapter from
Mastering Python Networking - Third Edition
Published in: Jan 2020
Publisher: Packt
ISBN-13: 9781839214677
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image