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.
Send command and send config are the tasks that you can see in every 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.
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.
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.
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.
And finally the configuration file, which introduces inventory files and enables multithreading in which we have discussed in the previous section.
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.
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.
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.
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.