Python3 Network Programming – Installing Python3-pip / Netmiko Module, writing Netmiko script, and executing it on the multi-switch lab / working through troubleshooting!

NetmikoAuto1

Now that I have secured SSH in my previous post on this lab, I will continue forward using Netmiko that utilizes SSH for connections, so I will get right to it here.

First to prep your NetAuto Host for Netmiko (this requires Internet Connection)

The NetAuto host in GNS3 does not come with Netmiko modules installed by default, so you have to run the following commands on the root prompt to install it:

apt-get update” – Pulls host updates in general for the NetAuto Host files, I’d do it even if you just did this yesterday, as can be seen here I actually did yesterday but today:

NetmikoAuto2

Always want to make sure your updating that host so you aren’t running into update-driven issues that take away from your labbing time, so be sure to run a quick update!

apt-get install python3-pip” – Its a very light weight install of the modules required for the NetAuto host to pull function from the netmiko library:

NetmikoAuto3

Oddly I did have this installed, but if yours doesn’t it will of course install the package, and you will be prompted for permissions to install it, which you will to enter “yes” to continue and finish the install.

“pip3 install -U netmiko” – This unpacks Netmiko and you are all set to go from here to run Netmiko scripts, but here its shown this NetAuto Host in GNS3 already has it unpacked and ready to go (GNS3 is really on top of its game!):

NetmikoAuto4

Now I will take a look at an initial script created using Netmiko, which in Visual Studio code will show an error, as shown below.

Reviewing the first script written utilizing Netmiko modules / SSH!

One quick note before proceeding – Avoid naming your scripts with automation tool keywords like “python” or “netmiko” as it may cause issues down the road with opening incorrect files because of the file name matching a python or netmiko file name.

Below is the first script I will be running using Netmike, though I am running Visual Studio Code on a Windows 10 machine so the top line here just inherently shows that there is a problem, however the NetAuto Host running Linux will understand it:

NetmikoAuto5

Again Netmiko and Python are largely aimed towards Linux Distros, which I have an Ubuntu VM, but to reduce load my CPU / RAM while labbing I am just working through what appears as errors in Windows knowing it will work on my Linux Host in GNS3.

From the start we start with “from netmiko import ConnectHandler” which is equivalent to our “import telnetlib” from previous scripts, this works just like an “import” command but is specific to calling out “netmiko” functions specifically.

This portion of the code below:

iosv_l2 = {
    ‘device_type’: ‘cisco_ios’,
    ‘ip’: ‘192.168.238.22’,
    ‘username’: ‘looped’,
    ‘password’: ‘back’,
}
This is creating a variable in Python3 that is using a dictionary, as can be seen in this Python Dictionary (which looks a lot like parts of JSON Key Pair strings) I will only be hitting SW2 with this script, however down the road we will of course be running scripts that allow multiple devices to be touched.
This variable is referenced in the next chunk of could, that first shows the VLANs created previously in the Telnet Automation labs that are currently configured:
net_connect = ConnectHandler(**iosv_l2)
output = net_connect.send_command(‘sh vlan brief’)
print (output)
This called out our variable to connect to, send a “sh vlan brief” and print the output.
Next I aim to configure both an loopback interface and VLANs 21-30 using a “for” and “range” loop / function:
config_commands = [‘int loop 0’, ‘ip address 2.2.2.2 255.255.255.255’]
output = net_connect.send_config_set(config_commands)
print (output)
for n in range (21,31):
    print (“Creating VLAN ” +str(n))
    config_commands = [‘vlan ‘ + str(n), ‘name Miko_Vlan ‘ + str(n)]
    output = net_connect.send_config_set(config_commands)
    print(output)
Then finally do a “sh vlan brief” and “sh ip brief” before doing an ‘end’ and ‘wr’ to write the new commands to the startup-config at the tail end of the script:
For some reason, SW2 and SW3 in this diagram are pingable, but showing SSH Connection refused both to my NetAuto Host and local laptop Putty:
sshlab8
Rather than removing / adjusting everything above, I wanted to point out this odd behavior as in the previous labs it could SSH fine one Telnet was disabled, so I will be switching the script to hit SW1 for now.
I have done some troubleshooting on this, reboot my entire laptop, and seems like I may just need to remove and reconfigure the switches, but I may be missing something so want to demo this first and troubleshoot that more in depth before deleting the devices.
So this will be the new script with the adjustment just to hit SW1 for now while I figure out what is going on with SW2 and SW3 refusing SSH Connections:
NewSSHScript
(And yes I do realize I spelled Meko wrong in the script name, but being its a lab I am not really worried about changing it to cover up my derp there) 🙂
Given I already have the script pasted into my NetAuto Host, its such a small change that I will just “nano FirstMiko.py” to adjust the IP, otherwise with larger changes I will generally just “rm -f (filename)” and re-create the file as its just easier for me to copy pasta from VSC to a new file than navigate Linux script files to make lots of changes.
Below I will copy / paste the entire results of the script running in its entirety
There is kind of a lot of odd behaviors which I am admittedly brand new to Netmiko and basically automation, so I want to save the entire output here for review:
Output from the above script being executed from NetAuto Host to SW1:

root@NetworkAutomation-1:~# python3 FirstMiko.py

VLAN Name Status Ports
—- ——————————– ——— ——————————-
1 default active Gi0/0, Gi0/1, Gi0/2, Gi0/3, Gi1/0, Gi1/1, Gi1/2, Gi1/3, Gi2/0, Gi2/1, Gi2/2, Gi2/3, Gi3/0, Gi3/1, Gi3/2, Gi3/3
2 Auto_Vlan_2 active
3 Auto_Vlan_3 active
4 Auto_VLAN_4 active
5 Auto_VLAN_5 active
6 Auto_VLAN_6 active
7 Auto_VLAN_7 active
8 Auto_VLAN_8 active
9 Auto_VLAN_9 active
10 Auto_VLAN_10 active
11 Auto_VLAN_11 active
12 Auto_VLAN_12 active
13 Auto_VLAN_13 active
14 Auto_VLAN_14 active
15 Auto_VLAN_15 active
16 Auto_VLAN_16 active
17 Auto_VLAN_17 active
18 Auto_VLAN_18 active
19 Auto_VLAN_19 active
20 Auto_VLAN_20 active
1002 fddi-default act/unsup
1003 token-ring-default act/unsup
1004 fddinet-default act/unsup
1005 trnet-default act/unsup
config term
Enter configuration commands, one per line. End with CNTL/Z.
SW1(config)#int loop 0
SW1(config-if)#ip address 1.1.1.1 255.255.255.255
SW1(config-if)#end
SW1#
Creating VLAN 21
config term
Enter configuration commands, one per line. End with CNTL/Z.
SW1(config)#vlan 21
SW1(config-vlan)#name Miko_Vlan 21
SW1(config-vlan)#end
SW1#
Creating VLAN 22
config term
Enter configuration commands, one per line. End with CNTL/Z.
SW1(config)#vlan 22
SW1(config-vlan)#name Miko_Vlan 22
SW1(config-vlan)#end
SW1#
Creating VLAN 23
config term
Enter configuration commands, one per line. End with CNTL/Z.
SW1(config)#vlan 23
SW1(config-vlan)#name Miko_Vlan 23
SW1(config-vlan)#end
SW1#
Creating VLAN 24
config term
Enter configuration commands, one per line. End with CNTL/Z.
SW1(config)#vlan 24
SW1(config-vlan)#name Miko_Vlan 24
SW1(config-vlan)#end
SW1#
Creating VLAN 25
config term
Enter configuration commands, one per line. End with CNTL/Z.
SW1(config)#vlan 25
SW1(config-vlan)#name Miko_Vlan 25
SW1(config-vlan)#end
SW1#
Creating VLAN 26
config term
Enter configuration commands, one per line. End with CNTL/Z.
SW1(config)#vlan 26
SW1(config-vlan)#name Miko_Vlan 26
SW1(config-vlan)#end
SW1#
Creating VLAN 27
config term
Enter configuration commands, one per line. End with CNTL/Z.
SW1(config)#vlan 27
SW1(config-vlan)#name Miko_Vlan 27
SW1(config-vlan)#end
SW1#
Creating VLAN 28
config term
Enter configuration commands, one per line. End with CNTL/Z.
SW1(config)#vlan 28
SW1(config-vlan)#name Miko_Vlan 28
SW1(config-vlan)#end
SW1#
Creating VLAN 29
config term
Enter configuration commands, one per line. End with CNTL/Z.
SW1(config)#vlan 29
SW1(config-vlan)#name Miko_Vlan 29
SW1(config-vlan)#end
SW1#
Creating VLAN 30
config term
Enter configuration commands, one per line. End with CNTL/Z.
SW1(config)#vlan 30
SW1(config-vlan)#name Miko_Vlan 30
SW1(config-vlan)#end
SW1#
Building configuration…
Compressed configuration from 3943 bytes to 1833 bytes[OK]
^X
Traceback (most recent call last):
File “FirstMiko.py”, line 29, in <module>
output = net_connect.send_command(‘exit’)
File “/usr/local/lib/python3.8/dist-packages/netmiko/utilities.py”, line 347, in wrapper_decorator
return func(self, *args, **kwargs)
File “/usr/local/lib/python3.8/dist-packages/netmiko/base_connection.py”, line 1429, in send_command
raise IOError(
OSError: Search pattern never detected in send_command_expect: SW1\#
root@NetworkAutomation-1:~#
root@NetworkAutomation-1:~#

There is a noticeable pattern of the script writing its own config term / end

Highlighted in red demonstrates a unique feature of netmiko’s script that I did not put anywhere for it to “config term” or “end” throughout the script, which I did not put in so I can only assume the script commands had sent those commands given that Cisco IOS was defined I assume it knew it had to use those functions to create the writes in the script however its interesting every “Iteration” of that “range” loop to create VLANs had it end / conf t ever time.

Highlighted in Blue is the final exit I assumed would just cleanly exit back to the NetAuto Host at the tail end was obviously not needed or even understood, so I assume that is not needed for the script to exit out of the SSH session, or perhaps a different command is needed to end the SSH session.

Though it was a bit messy, it did succeed in writing the desired configs to SW1!

Just looking at the terminal output from my console session shows the following:

sshlab9

 

And from SW1 CLI output:

SW1#sh ip int brief
Interface IP-Address OK? Method Status Protocol
GigabitEthernet0/0 unassigned YES unset up up
GigabitEthernet0/1 unassigned YES unset up up
GigabitEthernet0/2 unassigned YES unset up up
GigabitEthernet0/3 unassigned YES unset up up
GigabitEthernet1/0 unassigned YES unset down down
GigabitEthernet1/1 unassigned YES unset down down
GigabitEthernet1/2 unassigned YES unset down down
GigabitEthernet1/3 unassigned YES unset down down
GigabitEthernet2/0 unassigned YES unset down down
GigabitEthernet2/1 unassigned YES unset down down
GigabitEthernet2/2 unassigned YES unset down down
GigabitEthernet2/3 unassigned YES unset down down
GigabitEthernet3/0 unassigned YES unset down down
GigabitEthernet3/1 unassigned YES unset down down
GigabitEthernet3/2 unassigned YES unset down down
GigabitEthernet3/3 unassigned YES unset down down
Loopback0 1.1.1.1 YES manual up up
Vlan1 192.168.238.11 YES NVRAM up up
SW1#sh vlan brief

VLAN Name Status Ports
—- ——————————– ——— ——————————-
1 default active Gi0/0, Gi0/1, Gi0/2, Gi0/3
Gi1/0, Gi1/1, Gi1/2, Gi1/3
Gi2/0, Gi2/1, Gi2/2, Gi2/3
Gi3/0, Gi3/1, Gi3/2, Gi3/3
2 Auto_Vlan_2 active
3 Auto_Vlan_3 active
4 Auto_VLAN_4 active
5 Auto_VLAN_5 active
6 Auto_VLAN_6 active
7 Auto_VLAN_7 active
8 Auto_VLAN_8 active
9 Auto_VLAN_9 active
10 Auto_VLAN_10 active
11 Auto_VLAN_11 active
12 Auto_VLAN_12 active
13 Auto_VLAN_13 active
14 Auto_VLAN_14 active
15 Auto_VLAN_15 active
16 Auto_VLAN_16 active
17 Auto_VLAN_17 active

VLAN Name Status Ports
—- ——————————– ——— ——————————-
18 Auto_VLAN_18 active
19 Auto_VLAN_19 active
20 Auto_VLAN_20 active
21 Miko_Vlan 21 active
22 Miko_Vlan 22 active
23 Miko_Vlan 23 active
24 Miko_Vlan 24 active
25 Miko_Vlan 25 active
26 Miko_Vlan 26 active
27 Miko_Vlan 27 active
28 Miko_Vlan 28 active
29 Miko_Vlan 29 active
30 Miko_Vlan 30 active
1002 fddi-default act/unsup
1003 token-ring-default act/unsup
1004 fddinet-default act/unsup
1005 trnet-default act/unsup
SW1#

So I have a start into Netmiko, though I definitely have a ways to go!

I unfortunately did have to just end up rebuilding the lab to keep the forward progress moving, as can be seen here all three switches are once again reachable from the NetAuto Host via SSH for the upcoming labs:

SSHworking

I unfortunately lost all the files on my NetAuto Host as I had to rebuild the whole lab, as it would not connect after simply reloading / reconfiguration of SW2 and SW3 as they now have new Keys that the NetAuto Host will not allow a connection to as it detected the change in RSA Key – So for the sake of moving forward I just quickly spun up a new project in GNS3.

Still not sure what happened there as can be seen with the exact same configs SSH is working again, I assume its some kind of buggy config, however onward I march! 🙂

Back to labbing here, until next time!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s