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:
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:
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!):
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:
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:


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:
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:
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!