How to Automate Network Access With Python Netmiko Library part-1

We use the Python Netmiko Library to automate network access securely. Netmiko is a Python library that is used to build a secure remote connection with network devices using an SSH connection. Nemiko is not the default library in Python. you will have to install it manually in it. It is necessary to enable SSH on Routers and Switches because the netmiko library requires an SSH connection. To start the journey into Python Netmiko Network Automation, we have to install the Netmiko library using the PIP command. PIP, which stands for Pip Install Packages. Open the command prompt in Windows, and run the command, “python -m pip install” as in the following screenshot;

install PIP

If you want to check the PIP version, then run the command “PIP –version”;

PIP version

If you want to upgrade pip, then you can run the command;  

“python -m pip install –upgrade pip”

Upgrade PIP

Now, check the installed Python libraries using the command, “pip list”, which will list libraries that are installed in the system.

PIP list

In our case, we have installed it in our system, but you can install it via the following command, if it is doesn’t exist in your system, “PIP install netmiko” in the cmd;

Install netmiko

As SSH is necessary to be enabled on networking devices, for using the Netmiko library, because it uses SSH to establish connections with devices.  So, we are going to enable SSH on the Cisco device as below

Enable SSH on router

To check whether SSH is enabled or not. We use the command “show run | sec line vty” in the following screenshot;

show ssh

It is mandatory to assign hostname and domain name to the device because SSH uses the crypto key name, and that key name is generated from the hostname and domain name. If we generate an RSA key via the #crypto key generate rsa” command, then an output appears as “Please define a domain-name first” as in the following work:

So you should assign a domain name and hostname before creating the rsa key.

Assign Domain-name

 If you want to delete rsa key, then use the following command to remove it.

remove rsa

We can create rsa without a hostname and domain name through the following command. Also, confirm it by the command “show crypto key mypubkey rsa” in the following screenshot:

generate rsa without domain-nam

We must enable SSH on routers, and switches because it establishes SSH connections with devices. We enable SSH on Cisco devices (routers) in the following method. Also, configure hostname and domain name. You should confirm your hostname and domain name. In our case, the hostname is R1, while the domain name is marwat. After configuring the domain name, run the command “crypto key generates rsa modulus 1024. Then, it will create an RSA key. Also, configure username and password with privilege level 15.

Netmiko Library Lab Setup

As our lab is ready, now we are going to EVE-NG, and setting our lab in EVE-NG. We will perform this lab in GNS3 with simple steps. Just connect GNS3 with a PC or laptop, and run the script. We already published an article on how to connect GNS3 with a PC or to a real network.

Configure the IP address on fast ethernet 0/0 of the router interface. Also, configure other basic settings as required for running Netmiko script, which we have already explained in this article at the beginning, which are host-name, domain-name, user-name, password, generating RSA key, etc. Also, enable SSH on the router, because Netmiko is working with SSH.

We are going to show all the settings required for the running Netmiko script in the following screenshot.

Check the connectivity between the PC and the eve-ng router. We are using the ping command to ping it.

Let’s write the netmiko script. We have written the netmiko script in the vs code and, saved it as “netmiko1.py”.

Now run the Python script in the Windows command prompt (CMD), as given below in the diagram.

In most situations, we use a dictionary with the netmiko library as defined in the below script. Moreover, in this script, we are going to assign an IP address to the Loopback and fast ethernet 0/1 interfaces.

Run the netmiko2 script in the cmd. In the screenshot below, the script has assigned IP addresses to Loopback interface 1 and fast ethernet 0/1.

We are going to modify the existing script with a few changes. We have written only one username and one password for it. But, if there are multiple users at the same router, then what should you do?

Let’s try another script with a little modification in the script.

While the script is in the cmd as in the screenshot. This time we can enter different user names because we wrote a general script for it.

run the script

We noticed that there is no password visible here.

Similar Posts

Leave a Reply

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