top of page
  • Writer's pictureTaylor Etheredge

Introduction to Netmiko

Updated: Nov 10, 2023

Here we are in another post and this time it is not just going to be about Python itself. We are going to be writing about how to gather information from a Cisco networking device using Python.

The package we are going to use is called Netmiko, which is an interface for working with networking devices. We are only concerned with gathering data from the device for now.

First let's start with a new file called In that file we are going to import the methods and classes from the netmiko package, as well as the logging package.

from netmiko import ConnectHandler, redispatch
import logging

Then we set the logging to standard output in debug mode.


Now we need to create a NetDevice class for interacting with the networking device(s) in question.

class NetDevice():
    def __init__(self, device, config_file=None):
        if config_file is not None:
            self.config_file = config_file
            self.config_file = '~/.ssh/config'
        self.device = list(device.keys())[0]
        self.device_details = device[self.device]

In order to create a new NetDevice object we need to pass in a dictionary of device details. These details include the key as being the name of the device and the values are another dictionary containing the username, password and type of device. The type of device refers to the type that netmiko allows for, i.e. cisco-nxos. We can also pass in a custom ssh config file or use the default ssh config file if not passed in.

We then assign the instance variable of device to the dictionaries key. The we set the instance variable of device_details to the value which is another dictionary with details on how to connect to the device.

Next we create an instance method that defines the connection parameters for the device.

def connection_params(self):
    return {
        'device_type': 'linux_ssh',
        'host': self.device,
        'username': self.device_details['username'],
        'password': self.device_details['password'],
        'conn_timeout': 35,
        'banner_timeout': 60,
        'use_keys': False,
        'verbose': True,
        'ssh_config_file': self.config_file,
        'allow_agent': False,
        'disabled_algorithms': dict(pubkeys=["rsa-sha2-512", "rsa-sha2-256"]),
        'session_log': 'output.txt'

This returns a dictionary of key value pairs needed to connect to the device in question. As you can see we use the instance variables we defined above and associate their values to the keys need to create a connection.

def network_conn(self):
    return ConnectHandler(**self.connection_params())

Here we define a new instance method that returns a ConnectHandler object with the parameters that we specified above. This technique is know as a splat and can unpack the dictionary items to the correct items the ConnectHandler object needs.

Now for the gathering of data from the Cisco device in question. We are going to create one last instance method called get_vlan_brief. Here is what it will look like:

def get_vlan_brief(self):
    conn = self.network_conn()
    logging.debug(f"connected to {self.device_details['username']}")

    logging.debug(f"\n{'*'*10} Connected to net device: {self.device_details['username']} {'*'*10}\n")

    redispatch(conn, device_type=self.device_details['type'])
    vlan_data = conn.send_command("show vlan brief", use_textfsm=True)

    return vlan_data

First we set a variable named conn that calls out to method network_conn. This starts the network connection to the Cisco device. Then we wait for the prompt, log that we have established a connection to the device, and finally write to the device the password. Then we wait for the enable prompt, and then call the redispatch method to tell it the connection and the device type in order to pull the correct type. The here is the part we have been waiting for, the actual pulling information from the device. The variable vlan_data is set to the output of "show vlan brief" and the use_textfsm is set to True to format the data into Python data structures that is easily accessible. This is used so that you don't have to use regular expressions to parse the vlan brief information. Then we close the connection to the device and finally return the vlan_data variable.

I hope this help you get a feel for working with Netmiko to gather data from a networking device. Please stay tuned for more posts.

24 views0 comments


bottom of page