Table of Contents

Nornir netmiko is the first plugin that we’ll be discussing to send command to the router to monitor something.

In each plugin we have different tasks to send monitoring commands and configuration commands to the device.

In this section we have a configuration example to send monitoring commands via nornir netmiko plugin. The next section covers sending configuration commands via nornir netmiko.

Nornir Netmiko Plugin

The Netmiko plugin is important because it supports the largest number of devices.

If the device we are using on the network is not supported in the Scrapli plugin, the main plugin option is the Netmiko option, which supports 10 devices fully and supports about 100 devices partially, which we have explained in the introductory section.

 Before giving nornir netmiko send command configuration example, it might be useful to review the existing tasks in the nornir netmiko tasks and their parameters.

Through https://nornir.tech , nornir project and then plugins, we can find the source of every plugin.

The source of nornir netmiko is located here and task supported in this plugin are in tasks subfolder

Send command and send config are the tasks that you can see in every plugin.

Nornir Netmiko Plugin
Nornir Netmiko Plugin

In this plugin, the tasks “netmiko_send_command” and “netmiko_send_config” are the tasks to send monitoring commands and config commands.

In addition to these tasks, there are also some other tasks. The other important tasks are “netmiko_save_config” to save the configuration,  “netmiko_file_transfer” to file transfer between any two file systems or locations and “netmiko_commit” to commit the configuration changes in IOS XR or any device with commit capabilities.

In this section, we discuss “netmiko_send_command” and in the next section we will discuss “netmiko_send_config”.

The Task: “netmiko_send_command”

If we check inside “netmiko_send_command” task, the name of the task which is “netmiko_send_command” and “command_string”, the command itself, are the necessary parameters we must specify when using this task.

def netmiko_send_command(
    task: Task,
    command_string: str,
    use_timing: bool = False,
    enable: bool = False,
    **kwargs: Any
) -> Result:
    """
    Execute Netmiko send_command method (or send_command_timing)
    Arguments:
        command_string: Command to execute on the remote network device.
        use_timing: Set to True to switch to send_command_timing method.
        enable: Set to True to force Netmiko .enable() call.
        kwargs: Additional arguments to pass to send_command method.
    Returns:
        Result object with the following attributes set:
          * result: Result of the show command (generally a string, but depends on use of TextFSM).

Nornir Netmiko Send Command Configuration Example

Now we can start running the Python script that I’ve already prepared to send command to the device.

Review Inventory and Config Files

Inventory and configuration files are copied from the previous section. We have discussed the details of inventory and configuration files in the previous section.

In this section, let’s have a review before running the script.

We start with hosts.yaml file.

---
R1:
  hostname: "192.168.1.11"
#  username: "rayka"
#  password: "rayka-co.com"
  groups:
    - cisco

Only one device named R1 is configured inside this file. The device’s IP address, username and password must be configured in order to connect to the device.

Username and password are commented because they are also configured in the defaults.yaml file.

The device is also configured as a member of the cisco group.

Inside groups.yaml file, the “ios” platform is configured for the devices inside cisco group.

---
cisco:
  platform: ios

Inside defaults.yaml, username, password and platform is configured repetitive as a default parameters. Any deviating parameters can be configured in groups.yaml or hosts.yaml inventory files.

---
username: "rayka"
password: "rayka-co.com"
platform: "ios"

And finally the configuration file, which introduces inventory files and enables multithreading in which we have discussed in the previous section.

---
inventory:
  plugin: SimpleInventory
  options:
    host_file: "hosts.yaml"
    group_file: "groups.yaml"
    defaults_file: "defaults.yaml"

runner:
  plugin: threaded
  options:
    num_workers: 10

Install Required Plugins to run Nornir Netmiko Scripts

To run the script you have to make sure that required plugins are alrea installed in your controller.

This is the note that I have prepared not to forget to install he required plugins.

sudo apt-get update
sudo apt-get install python3 python3-venv python3-pip

python3 -m pip install nornir
python3 -m pip install nornir-netmiko
python3 -m pip install nornir-utils

After updating application repository, make sure that python3 and python3-pip are installed in your controller. python3-venv is important when you want to run the script in an isolated virtual environment which is recommended.

Isolated virtual environment, helps you not to make any changes to your main operating system configuration files.

The other benefit is that sometimes you need to run a script with a configuration that is incompatible with another script. Therefore, it can run in an isolated virtual environment no to impact other scripts.

We then install required plugins in Python3 via the PIP. “nornir” and “nornir-netmiko” must of course be installed in order to send commands via the nornir-netmiko plugin.

The “nornir-utils” plugin is used for many basic tasks. What we use in this script and most of the scripts throughout the course is to print the result of running the Nornir script through running the “print_result” task.

Send Single Monitoring Command through Nornir Netmiko Plugin

This is the script that I have already prepared to send a single monitoring command through nornir netmiko plugin.

from nornir import InitNornir
from nornir_netmiko.tasks import netmiko_send_command
from nornir_utils.plugins.functions import print_result

nr = InitNornir(config_file="config.yaml")

def netmiko_send_commands_example(task):
    task.run(task=netmiko_send_command, command_string="show ip int brief | exc unass")

results=nr.run(task=netmiko_send_commands_example)
print_result(results)

The same script structure will be used during the course and in all plugins and tasks.

In the first three lines, only the required tasks of the installed plugins are imported.

The task “InitNornir” to initialize nornir. Always we use this task in nornir plugin.

The task “netmiko_send_command” to send the command through netmiko plugin and the tak “print_result” from “nornir_utils” plugin to print the result of running command.

Then we initialize the nornir with the config file that we have already pepared.

Then we configure a function with two required parameters, the name of the task, “netmiko_send_command”, and the command that must be run in the final devices.

As an example we run the command to show layer 3 interfaces with assigned IP addresses.

Then we run the function through initialized nornir.

In the last line we print the result of the running command with using “print_result” function form “nornir_utils” plugins.

You can run the script with many commands to see the result of each command sent.

majid@majid-ubuntu:~/devnet/pyhton_nornir/2023/2.nornir_netmiko$ python3 2.nornir_netmiko_send_command.py
netmiko_send_commands_example***************************************************
* R1 ** changed : False ********************************************************
vvvv netmiko_send_commands_example ** changed : False vvvvvvvvvvvvvvvvvvvvvvvvvv INFO
---- netmiko_send_command ** changed : False ----------------------------------- INFO
Interface              IP-Address      OK? Method Status                Protocol
GigabitEthernet1       192.168.1.11    YES manual up                    up

^^^^ END netmiko_send_commands_example ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Send multiple Monitoring Command

The last script that I am going to run in this section is to send more than one commands to final device.

The script is exactly the same. The only difference is that we put the commands inside a list and then we run the commands one by one inside a loop.

from nornir import InitNornir
from nornir_netmiko.tasks import netmiko_send_command
from nornir_utils.plugins.functions import print_result

nr = InitNornir(config_file="config.yaml")
commands=["show ip int brief | exc unass", "show version | inc XE"]

def netmiko_send_commands_example(task):
    for command in commands:
        task.run(task=netmiko_send_command, command_string=command)

results=nr.run(task=netmiko_send_commands_example)
print_result(results)
majid@majid-ubuntu:~/devnet/pyhton_nornir/2023/2.nornir_netmiko$ python3 2.1.nornir_netmiko_send_commands.py
netmiko_send_commands_example***************************************************
* R1 ** changed : False ********************************************************
vvvv netmiko_send_commands_example ** changed : False vvvvvvvvvvvvvvvvvvvvvvvvvv INFO
---- netmiko_send_command ** changed : False ----------------------------------- INFO
Interface              IP-Address      OK? Method Status                Protocol
GigabitEthernet1       192.168.1.11    YES manual up                    up

---- netmiko_send_command ** changed : False ----------------------------------- INFO
Cisco IOS XE Software, Version 17.01.01
Cisco IOS Software [Amsterdam], Virtual XE Software (X86_64_LINUX_IOSD-UNIVERSALK9-M), Version 17.1.1, RELEASE SOFTWARE (fc3)
Cisco IOS-XE software, Copyright (c) 2005-2019 by cisco Systems, Inc.
All rights reserved.  Certain components of Cisco IOS-XE software are
documentation or "License Notice" file accompanying the IOS-XE software,
or the applicable URL provided on the flyer accompanying the IOS-XE
ROM: IOS-XE ROMMON
cisco CSR1000V (VXE) processor (revision VXE) with 2078006K/3075K bytes of memory.
^^^^ END netmiko_send_commands_example ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
majid@majid-ubuntu:~/devnet/pyhton_nornir/2023/2.nornir_netmiko$

You can download nornir netmiko send command configuration example from this link.

Back to: CLI based Network Automation using Python Nornir > Python Nornir Netmiko Plugin

2 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *


Post comment