The best place for network developers

Netmiko – Python Library

Introduction to Netmiko Library – how to play with younger brother of Paramiko Library?

If you are reading this article, you probably know that connecting to a network device via SSH and playing with Python can be quite easy. What is more, it can be done with a lot of different ways and with different libraries in Python – this time we will present you a Netmiko Library – successor of Paramiko.

What is Netmiko library?

Netmiko is multi-vendor python library which enables SSH connections to network device. What is important, Netmiko library is build on Paramiko Python library and was created to simplify this library.

https://pypi.org/project/netmiko/

More theoretical details about Netmiko you can read on: https://github.com/ktbyers/netmiko. 

How does netmiko work? First of all, as always, we have to install the library:

pip install netmiko

What is important, Netmiko installs some additional packages:

  • Paramiko >= 2.4.2
  • scp >= 0.10.0
  • pyyaml
  • pyserial
  • textfsm

When this is done, we can begin playing with Netmiko. As a first step we import libraries:

from netmiko import ConnectHandler

This is the Netmiko library used to SSH connection to network device.

 

  1. cisco_router = {
  2. 'device_type': 'cisco_ios',
  3. 'ip': '10.10.10.1',
  4. 'username': 'cisco',
  5. 'password': 'cisco',
  6. # 'port' : 22, # optional - defaults is 22
  7. # 'secret': '', # optional - defaults is ''
  8. # 'verbose': False, # optional - defaults is False
  9. }

Here we define a dictionary where we declare variables to make a SSH connection.

The way how we create a connection to network device, from the programmatic point of view, is completely different in comparison to Paramiko.

The dictionary contains the following keys:

  • Device_type define what type of device we are connecting to. The list is predefined, full list is on https://github.com/ktbyers/netmiko/blob/master/netmiko/ssh_dispatcher.py#L70
    In our lab we use cisco_ios as a device that we connect to.
  • Next 3 keys are mandatory and we define: ip of the device, our username and password. Providing username and password also can be done as a getuser and getpass using Python built-in methods or just providing them as „plain text” in your script.
  • Last 3 keys are optional, we left them here just for your information that you can change these settings, but we use default values in our scripts.
net_connect = ConnectHandler(**cisco_router)

In Netmiko we state only dictionary (cisco_router is our dictionary) where we fulfill all mandatory fields and then, connection will be made via above command. All other „magic” about how netmiko connects to device via SSH (you cal also use other ports like telnet) is handled by Netmiko and… this is why a lot of us love Netmiko. You do not have to care about anything else.

If you do not know what are ** before our defined earlier dictionary (**cisco_router), do not worry. Those are kwargs – this is a „magical variable”, you can read more about it (and how to use it) on http://book.pythontips.com/en/latest/args_and_kwargs.html

If you do not want to go into details, please just think about it as „I have to provide my dictionary name in object: ConnectHandler(**)

net_connect is a variable that we have created and will use to connect to e.g. send a command, configure device with list of commands etc.

  1. output = net_connect.send_command('show ip int brief')
  2. print(output)

Here we define variable output. It is also good to know that Netmiko library uses „return” option in its’ send_command method. As a result, we do not have to care about „send” and „recv” methods in comparison with Paramiko library.

Output from this command runs as follow:

IP Interface Status for VRF "default"(1)
Interface            IP Address      Interface Status
Lo0                  192.168.0.1     protocol-up/link-up/admin-up       

As a result, we receive a „clear” output from device. We do not have to care about „hostname#” and any parsing output. This is huge benefit in comparison with Paramiko.

  1. output = net_connect.send_command('sh int loopback 0 description')
  2. print(output)
  3. config_commands = [ 'int loopback 0',
  4. 'description DESCRIPTION MADE BY NETMIKO']
  5. output = net_connect.send_config_set(config_commands)
  6. print(output)
  7. output = net_connect.send_command('sh int loopback 0 description')
  8. print(output)

As a next example of how Netmiko library works, we will change descritpion on the  interface Loopback 0. Line 1 does the same thing as the previous example, it logs on device, does a command „sh int loopback 0 description”. We do it to verify what is current description on int Loopback0:

-------------------------------------------------------------------------------
Interface                Description                                            
-------------------------------------------------------------------------------
loopback0                --
 

As we can see in the output, there is currently no description on this interface. Via commands in lines 3 and 4 we define a list of commands that will be sent to a device. Those commands change description on this interface. This is done via send_config_set method, this is also built-in method in Netmiko library.

The output from sending commands is the following:

config term
Enter configuration commands, one per line. End with CNTL/Z.
 
nx-osv9000-1(config)# int loopback 0
 
nx-osv9000-1(config-if)# description DESCRIPTION MADE BY NETMIKO
 
nx-osv9000-1(config-if)# end
 
nx-osv9000-1# 

As we can see, Netmiko automatically returns all commands. Also returned is full output structure with information which commands and how they were applied on our CLI. What is more, please make note that Netmiko automatically goes into „conf t” module and at the end it adds in CLI „end” command.

Last thing that we have done is verifying if our description has changed on port:

Interface                Description                                            
-------------------------------------------------------------------------------
loopback0                DESCRIPTION MADE BY NETMIKO

as a result, we can see that our script works perfectly and out current descirption on port is chagned.

Last but not least, here you can find full script used in this article. What is more you can also visit our github: https://github.com/DevNetSpace/Articles/blob/master/netmiko_library.py , where we collect all our scripts.

Access Point – automatic report

Do you remember when last time local IT / your colleague / HelpDesk / anyone asked you for gathering a report about some devices which are connected in your network? Do you remember all the boring stuff that you had to do? Login into 5, 10, 15… 50 devices and gathering information like IP, port number, description, device name, mac address?

Building lab environment

Every time I have to automate specific kind of a job I write down and answer project specific questions, which help me to prepare algorithm and general idea, how a new solution will work.

Of course, this list can be easily extended during algorithm preparation. A good approach is to prepare small working solution and extend it, every time you need it, by adding new functionality. Building small peaces and extracting specific functions will help in troubleshooting, reduce downtime during writing a code and of course, give a better planning and estimation of time when whole project will be finished.

Automatic interface configuration

For all of us entering same commands / text into CLI becomes boring after some time. Do you remember your fascination when you created a VLAN 10, connected two switches together and they pinged each other? Yes, probably for most of us were very enthusiastic that time. But after some time copying same command, creating same VLANs can become a little bit boring. What can we do to make it faster, better and less prone to mistakes?