Category: DevNet

Top 6 Python modules for network programming

In the track of both Network Automation and programming using python, there is some necessary modules you should know. If you have already visited “Necessary python modules for DevNet and Cisco Programming” ,perhaps you also know these 6 modules.

List of Top 6 python modules for network programming :

1. Netmiko

 Netmiko is simple and multi-vendor SSH connection to network devices and certainly one the greatest modules for network programming. In addition It is based on paramiko but hide paramiko’s complexity. we have written about differences between Netmiko and Paramiko before (Netmiko vs Paramiko). Additionally Netmiko supports only SSHv2 (not v1).

Finally here is an example of Netmiko :

from netmiko import ConnectHandler

password = "123456"
info = {
         'device_type': 'cisco_nxos',
         'host': '172.16.0.1',
         'username': 'cisco',
         'password': password,
}
net_connect = ConnectHandler(**info)
s = net_connect.send_command('show ip int br')
net_connect.disconnect()

print(s)

In conclusion of example above, we passed username (cisco) and password (123456) to cisco NXos device (172.16.0.1) and executed “show ip int br” command. Obviously the result of Python code is , “SHOW IP INTERFACE BRIEF” output.

Similarly, in case of different vendors , you can pass the variable “device_type” to specify vendor.

Also you can read more about Netmiko in url below :

Introduction to Netmiko

2. Pysnmp

PySNMP is a cross-platform and also pure-Python SNMP engine implementation. In detail it features fully-functional SNMP engine capable to act in Agent/Manager/Proxy roles, talking SNMP v1/v2c/v3 protocol versions over IPv4/IPv6 and other network transports. (Based on official site)

For instance here is an example :

# Callback function for receiving notifications
def cbFun(snmpEngine,
                   stateReference,
                   contextEngineId,
                   contextName,
                   varBinds,
                   cbCtx):
    transportDomain, transportAddress = snmpEngine.msgAndPduDsp.getTransportInfo(stateReference)

Also you can learn about pysnmp usage in cisco devices :

snmp query of cisco devices using pysnmp

pysnmp and cisco devices examples

3. NAPALM

NAPALM stands on Network Automation and Programmability Abstraction Layer with Multivendor support and is another  multivendor (Obviously!)  python library for automation.

Until now and at the time of writing, it supports following devices (read more):

  • Cisco IOS
  • Cisco IOS-XR
  • Cisco NX-OS
  • Arista EOS
  • Juniper JunOS

Of course we can choose which driver we want to work around and import.

The following is an example of Napalm :

import napalm

password = "123456"

driver = napalm.get_network_driver('iosxr')
device = driver(hostname='172.16.0.1', username='cisco', password=password)
device.open()
return_dictionary = device.cli(['show ip int br', ])
device.close()

s = return_dictionary['show ip int br']
print(s)

Furthermore you can follow NAPALM articles and examples :

NAPALM Introduction

4. nornir

Nornir is an automation framework created in python and it requires Python 3.6.2 or higher.It provides an abstraction for inventory (hosts and groupd) and concurrent task execution. Shortly the greatest advantage is that it’s very fast due to use threads. Besides you can find more information here.

Nornir is great and allows you to utilise already tried and tested Python libraries such as Netmiko and NAPALM.

However you can learn more :

Nornir introduction

Nornir and Cisco devices

Nornir vs Ansible

5. Unittest

Whether you are a network programming or programmer , you need to test codes you’ve written. Unittest is a unit testing framework and includes both testing framework and test runner.

Although we have explained it in detail:

 
6. Pyats

Finally , pyATS is an end-to-end testing ecosystem, specializing in data-driven and reusable testing, and engineered to be suitable for Agile, rapid development iterations. Extensible by design, pyATS enables developers start with small, simple and linear test cases, and scale towards large, complex and asynchronous test suites.

However pyATS is initially developed internally in Cisco, and is now available to the general public starting late 2017 through Cisco DevNet. Visit the pyATS home page at https://developer.cisco.com/site/pyats/

Additionally you can find more ,including examples :