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

In the previous article How to Automate Network Access With Python Netmiko Library part-1, we have called only one device at a time. But, this time, we will call more than one device at the same time in a single script to automate Network Access. We will mention device IP addresses in the list data type and then will call this list in a for loop. The for loop will run the first device IP, which will also run the associated commands, and then it will run the 2nd and 3rd IPs one after another.

Lab Topology for Automating Network Access

Our lab topology consists of three routers, and we connect routers directly to the Cloud (Laptop). These routers are R1, R2 and R3. We mentioned all the IP addresses in the screenshot below. The cloud has three network Loopback adopters, which are Ethernet 5, Ethernet 9 and Ethernet 10.  We have already published an article on how to connect the cloud (real network) with GNS3.

On the other hand, we have three Microsoft KM-Test Loopback adapters, through which we connect to the routers R1, R2, and R3. These Loopback adapters are ethernet 5, ethernet 9, and ethernet 10. We connected R1 to the ethernet 5, R2 to the ethernet 9, and R3 to the ethernet 10.

The Basic configuration of the routers R1, R2, and R3 are below in the screenshots one by one.




Explanation of the Script

In the following lines 1 and 2, the script imports the ConnecHandler class in the netmiko library which is used to establish an SSH connection with devices. The 2nd line imports the getpass library. We use the getpass library to hide passwords from being visible. Normally, when we put a password in Cisco devices, it is not visible to users.

Line 4 uses a variable Device, which consists of a list data type and has 3 IP addresses. The first one is, and the 2nd one is while the third one is

Username and Password in lines 6 and 7 will prompt for input username and password. The password will not be visible, because it uses the getpass library. The function of the getpass library is to hide passwords from being visible.

We defined for loop in line 7. The for loop uses a variable x. The x will get three IP addresses from a variable Device_IP. It will call, and one after another.

We defined a variable ‘machine’, which is a dictionary data type. The dictionary variable machine will call IP from the for loop with a variable Device_IP. The dictionary variable ‘machine’ has parameters like IP, username, password, port number, and device_type. The default port for SSH is ‘22’, but you can change it for security reasons. Similarly, the device type is Cisco iOS.

The “ConnectHandler” class establishes an SSH connection with the network device using the configuration specified in the ‘machine’ dictionary. The code will store the result in the ‘ssh’ variable. The print command will print “SSH connection established with the device having IP address: “). The display is a variable that will store the ‘output command’ which will be sent by the ssh.send.command. In ssh.send.command, ssh is a variable that represents SSH connection to the network device. While send.command is a method call on the ‘ssh’ object. This method sends the command ‘show IP interface brief’ to the network device. The ‘show ip interface brief’ is a command that is being sent to the Cisco networking device. Also note that, ‘send.command’ is used to send a single command to the network devices. But, if more than one command is being sent to the device, follow the next script in this article.

 Lastly, we are going to complete the script and present it on one screen as in the below screenshot.

Running the Script

Run the script in the command prompt. We saved our script as ‘’. Write py followed by and hit enter as in the below screenshot.

Now we are going to modify the script, The modification is to run multiple commands at the same time to different devices as under in the screenshot. We will create a variable “commands” and we will declare the variable ‘commands’ as a list data type, in which we will define two commands, which are (‘show ip interface brief’, and ‘show clock’).  We will use ssh.send.multiline() method to send multiple commands instead of one. However, we use ssh.command() method to send only one command.

Run the script to check it result on the screen.

Through this script, we automate network access with easy steps.

Similar Posts

Leave a Reply

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