NETMIKO: Hands On Lab III
In this lab, we will create a nested for loop, so a for loop with another for loop nested inside to do the same job as previous two labs.
The full code is below which I will break into sections for ease of understanding:
Line one below is basically calling the netmiko library module that was created by Kirk Byers. In the netmiko module, there are libraries that is created for network automation. And the SSH module is called ConnectHandler. It is designed to allow SSH connections for logging into a device. What I am asking is to use the Netmiko library (I previously installed already) and to use specifically the ssh module called Connecthandler.
Line 3 is where I create the list of routers called ‘router_ips’.
Lines 5-10 is where I create another list for the commands that I want to execute once I ssh into the routers. The list of the commands I want to execute is called ‘commands’.
Line 12 is where I create the first outer for loop, called ‘ip’ which will call in the ‘router_ips’ list and that contains the list of router IP addresses to ssh into. Line 13 will then display the message in a new line advising what router it is connecting to. Using a f-string allows you to call the for loop ‘ip’ and that will display the list of IP addresses as it is then calling in ‘router_ips’. Everything below will all be nested now in the ‘ip’ for loop.
Line 15 to 22 is where it is nested, we create the dictionary that includes the details required for the module ConnectHandler. Notice in lin 17 I am calling the value ‘ip’ which is the for loop ‘ip’ and that in turn is calling in the ‘router_ips’ list. So that is where it gets the IP addresses of the routers.
Line 23 is then creating the variable which is to ssh into the routers (remember the ‘router’ is the dictionary with all the relevant information required to ssh into the devices). The ssh variable is called ‘net_connect’ and is also nested in the ‘ip’ for loop (outer for loop).
Line 25 will execute the enable mode within the router using keyword/function .enable().
Line 27 – 30 is the inner nested for loop. Line 27 begins with the nested for loop called ‘command’ and will call in the list of commands created called ‘commands’.
Line 28 will then connect to the routers one by one and send the commands, remember ‘command’ is the inner for loop, which is then calling in ‘commands’ which has the list of commands I want to execute. The command output is stored in ‘output’ using the .send_command keyword. ‘net_connect’ remember is the variable used to ssh initially into the router using ConnectHandler.
Line 29 will then display the message ‘Command:’ and it will print the list of commands followed by the IPs of the router.
Line 30 will then print out the result stored in ‘output’ onto the screen.
Line 32 – once the commands have been sent and displayed, then disconnect the ssh session for security purposes.
And finally, to complete the script, line 34 just displays a message to say ‘Disconnected from’ the routers IP addresses. the {ip} is being called as an f-string and ‘ip’ is called from the outer for loop (‘for ip in router_ips’).
Result below: