How To Automate Network Access With Python Netmiko Library Part-3

In the last two articles about the Python netmiko library part 1 and part 2, we have discussed the basis of the netmiko library and its implementation in the article. In this article, we are focusing on “how to automate network” by modifying the netmiko library, introducing a few challenges to enhance your understanding. We will create a series of commands in a list data type and then will use this list with the for loop to execute one command at a time.

Topology overview of how to automate network

We directly connected the Cisco router R1 to a real PC through the cloud. However, the IP address configuration of a Router and cloud is as under in the screenshot.

We directly connected the Cisco router R1 to a real PC through the cloud. However, the IP address configuration of a Router and cloud is as under in the screenshot.

The ethernet 5 IP address is as under,

Ethernet 5 setting

Basic Configuration of a Router

Router R1 setting

Writing Python Script

We will explain the Python script to tell the users how to automate network.

importig and username

Import the Connect handler method from the netmiko library and then use the getpass method to secure a password. Use a variable ‘machines’, which consists of an IP address on a list. After that, enter your username and password.

for loop for device components

We use a for loop to retrieve the IP address from ‘machines’ by the use of a variable ‘My Device’. The for loop contains a variable ‘host’, which is of dictionary data type. It has different parameters, such as ip, username, password, port, and device_type.

ssh=connecthandler

“. At the last use ‘ConnectHandler(**host)’, to establish ssh connection with the device. This line says for the SSH connection. SSH is a variable and ConnectHandler is a module that is used to establish a ssh connection. We used double asterisks “**” with the ‘host’ dictionary variable for the purpose of unpacking the dictionary variable ‘host’. At the last, we used print(“SSH connection is established”), to print a message for the user.

ssh=connecthandler

These are the values (commands), which I want to retrieve from the Cisco router. For this, we will use “for loop”.  create a list data type variable ‘our_commands’ which will consist of various Cisco router commands, as in the following code.

for loops for displaying commands

In the above Python script, initiate another for loop. Within this loop, introduce a variable named ‘command’ that will iterate through values sourced from the ‘our_commands’ variable-a list of a specified data type. After the for loop, use the print function to display the command. It will tell to the user that I am printing the said command in {command}. So the {command} will print the ‘show version’ command first, after it, it will print the ‘terminal length 0’ command in the order in which it has been written.

            The ‘f’ in print tells about the format command, while ‘\n’ prints the output on the next line, so that one output will separate from another output.

            After all, the else: part will run and will tell the for loop, that there doesn’t exist more commands to send to the device or to print it.

python script for "how to automate networ"

Run the script

run the script

In the script, we observed that it separates the output of one command from another incoming command. Moreover, it also displays a message for the incoming printing command that follows:. If you intend to expand the list of devices in the ‘machines’ category, simply append their IP addresses to the ‘machines’ variable.

Similar Posts

Leave a Reply

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