Being as I have been tanking through just touching network devices with Python, I wanted to take a step back, and look at the components within the script that call out each others to function properly and how to start thinking about writing scripts freestyle that suits your needs and begin thinking diving deeper into Python!
This beginning of this script to shown starts with Python “Dictionaries”
First I want to review the top of the script, which are both variables and dictionaries:
Here we are first assigning these values such as “iosv_l2_s1” as a variable that = a Python Dictionary, which is indicated by the initial { after the = operator.
Dictionaries in Python are used to specify an item to its corresponding value, such as ip: (ipaddy) / username: looped / password: back / etc, and is important to understand that the open curly bracket { tells python you are going to start defining a Dictionary!
The Dictionary items must be indented just like a script, and once you are finished entering items into the Dictionary, to indicate this using a closed curly bracket } in its own line directly below the last Dictionary item!
While making three devices three separate chunks of code with the username / password in plain text is obviously not ideal, this is purely to visually demonstrate this concept.
Note – The final comma in these dictionaries is not actually needed, as a comma tells Python there is an additional Dictionary item, so I removed them from my script:
The extra commas won’t hurt anything by leaving them in there, but if you are sure the final item will ultimately be the final dictionary item in the script, it is OK to delete the final comma from the Dictionary entry before the close bracket below it }.
I then use these variables / dictionaries in a Python List to reference all devices
Below I create a Python “List” to be called out in the script to reference all of this info:
With Python Lists, we use regular brackets [ ] to indicate starting and ending a List!
Here we are creating another variable that references a Python “List” which is a collection of items to be referenced, which in this case it references our variables that contain dictionaries of information to be used further down in the script.
Next this list is referencing in a Loop to Create VLANs on Multiple Switches
Below is a for loop that references the list which references the dictionaries created:
At this point I actually noticed a goof up, as when I was writing this code I did not indent line 29 as I should have, being that it is the contents of another “for” loop nested within another “for” loop, so let me correct that quick:
This is one reason I love Visual Studio Code, is it will visually show when things are not correct, and is very helpful in the learning of Python coding as it is by no means an easy task to learn Python scripting from scratch!
However getting back on track, we see that the “for” loop in Line 26 references the List we made previously, that references all of our Dictionary entries as well.
Then I just copy / pasted the create VLAN “for n in range …” from another script, which should now be an executable script I can run on my 3 switch lab in GNS3.
Now it is time to run this script to see if I am full of it, or got all that right! 🙂
I was thinking about how to use this logic to possibly create loopback interfaces with unique addresses on each device, I know how to do it more manually, but for now I will just see if this script works to all my devices in GNS3, and threw a “wr” at the end of the script to write the configs to startup-config once finished, I am hoping this is place correctly within the script:
This seems logically correct given its indentation, it is part of the “all_devices” for Loop but outside of the “for / range” loop, so lets give it a spin here on the NetAuto Host.
Note – This will be a ton of output I will be Copy / Pasting for review below!
root@NetworkAutomation-1:~#
root@NetworkAutomation-1:~#
root@NetworkAutomation-1:~# python3 NextMiko.py
Creating VLAN 2
config term
Enter configuration commands, one per line. End with CNTL/Z.
SW1(config)#vlan 2
SW1(config-vlan)#name Miko_Vlan 2
SW1(config-vlan)#end
SW1#
Creating VLAN 3
config term
Enter configuration commands, one per line. End with CNTL/Z.
SW1(config)#vlan 3
SW1(config-vlan)#name Miko_Vlan 3
SW1(config-vlan)#end
SW1#
Creating VLAN 4
config term
Enter configuration commands, one per line. End with CNTL/Z.
SW1(config)#vlan 4
SW1(config-vlan)#name Miko_Vlan 4
SW1(config-vlan)#end
SW1#
Creating VLAN 5
config term
Enter configuration commands, one per line. End with CNTL/Z.
SW1(config)#vlan 5
SW1(config-vlan)#name Miko_Vlan 5
SW1(config-vlan)#end
SW1#
Creating VLAN 6
config term
Enter configuration commands, one per line. End with CNTL/Z.
SW1(config)#vlan 6
SW1(config-vlan)#name Miko_Vlan 6
SW1(config-vlan)#end
SW1#
Creating VLAN 7
config term
Enter configuration commands, one per line. End with CNTL/Z.
SW1(config)#vlan 7
SW1(config-vlan)#name Miko_Vlan 7
SW1(config-vlan)#end
SW1#
Creating VLAN 8
config term
Enter configuration commands, one per line. End with CNTL/Z.
SW1(config)#vlan 8
SW1(config-vlan)#name Miko_Vlan 8
SW1(config-vlan)#end
SW1#
Creating VLAN 9
config term
Enter configuration commands, one per line. End with CNTL/Z.
SW1(config)#vlan 9
SW1(config-vlan)#name Miko_Vlan 9
SW1(config-vlan)#end
SW1#
Creating VLAN 10
config term
Enter configuration commands, one per line. End with CNTL/Z.
SW1(config)#vlan 10
SW1(config-vlan)#name Miko_Vlan 10
SW1(config-vlan)#end
SW1#
Creating VLAN 2
config term
Enter configuration commands, one per line. End with CNTL/Z.
SW2(config)#vlan 2
SW2(config-vlan)#name Miko_Vlan 2
SW2(config-vlan)#end
SW2#
Creating VLAN 3
config term
Enter configuration commands, one per line. End with CNTL/Z.
SW2(config)#vlan 3
SW2(config-vlan)#name Miko_Vlan 3
SW2(config-vlan)#end
SW2#
Creating VLAN 4
config term
Enter configuration commands, one per line. End with CNTL/Z.
SW2(config)#vlan 4
SW2(config-vlan)#name Miko_Vlan 4
SW2(config-vlan)#end
SW2#
Creating VLAN 5
config term
Enter configuration commands, one per line. End with CNTL/Z.
SW2(config)#vlan 5
SW2(config-vlan)#name Miko_Vlan 5
SW2(config-vlan)#end
SW2#
Creating VLAN 6
config term
Enter configuration commands, one per line. End with CNTL/Z.
SW2(config)#vlan 6
SW2(config-vlan)#name Miko_Vlan 6
SW2(config-vlan)#end
SW2#
Creating VLAN 7
config term
Enter configuration commands, one per line. End with CNTL/Z.
SW2(config)#vlan 7
SW2(config-vlan)#name Miko_Vlan 7
SW2(config-vlan)#end
SW2#
Creating VLAN 8
config term
Enter configuration commands, one per line. End with CNTL/Z.
SW2(config)#vlan 8
SW2(config-vlan)#name Miko_Vlan 8
SW2(config-vlan)#end
SW2#
Creating VLAN 9
config term
Enter configuration commands, one per line. End with CNTL/Z.
SW2(config)#vlan 9
SW2(config-vlan)#name Miko_Vlan 9
SW2(config-vlan)#end
SW2#
Creating VLAN 10
config term
Enter configuration commands, one per line. End with CNTL/Z.
SW2(config)#vlan 10
SW2(config-vlan)#name Miko_Vlan 10
SW2(config-vlan)#end
SW2#
Creating VLAN 2
config term
Enter configuration commands, one per line. End with CNTL/Z.
SW3(config)#vlan 2
SW3(config-vlan)#name Miko_Vlan 2
SW3(config-vlan)#end
SW3#
Creating VLAN 3
config term
Enter configuration commands, one per line. End with CNTL/Z.
SW3(config)#vlan 3
SW3(config-vlan)#name Miko_Vlan 3
SW3(config-vlan)#end
SW3#
Creating VLAN 4
config term
Enter configuration commands, one per line. End with CNTL/Z.
SW3(config)#vlan 4
SW3(config-vlan)#name Miko_Vlan 4
SW3(config-vlan)#end
SW3#
Creating VLAN 5
config term
Enter configuration commands, one per line. End with CNTL/Z.
SW3(config)#vlan 5
SW3(config-vlan)#name Miko_Vlan 5
SW3(config-vlan)#end
SW3#
Creating VLAN 6
config term
Enter configuration commands, one per line. End with CNTL/Z.
SW3(config)#vlan 6
SW3(config-vlan)#name Miko_Vlan 6
SW3(config-vlan)#end
SW3#
Creating VLAN 7
config term
Enter configuration commands, one per line. End with CNTL/Z.
SW3(config)#vlan 7
SW3(config-vlan)#name Miko_Vlan 7
SW3(config-vlan)#end
SW3#
Creating VLAN 8
config term
Enter configuration commands, one per line. End with CNTL/Z.
SW3(config)#vlan 8
SW3(config-vlan)#name Miko_Vlan 8
SW3(config-vlan)#end
SW3#
Creating VLAN 9
config term
Enter configuration commands, one per line. End with CNTL/Z.
SW3(config)#vlan 9
SW3(config-vlan)#name Miko_Vlan 9
SW3(config-vlan)#end
SW3#
Creating VLAN 10
config term
Enter configuration commands, one per line. End with CNTL/Z.
SW3(config)#vlan 10
SW3(config-vlan)#name Miko_Vlan 10
SW3(config-vlan)#end
SW3#
root@NetworkAutomation-1:~#
Success! I have color coded the 3 different switches to tell their output apart!
One thing to note is that there is no “wr” seen on the output that was shown on the NetAuto Host, but that is simply because I did not put the print statement after it, as I went to one of my switches to check out the output and found the following:
And always verify verify verify when making config changes / updates:
That in Networking Terminology, is what you call, a good deal! 🙂
With that I will conclude this article, as this review for me was helpful, and I hope to anyone reading this it also helped link some concepts together as well!
I will be completely honest, in the beginning of learning DevNet I was optimistic and having fun learning completely new material, but Python was a road block big time.
Using it to program network devices via David Bombals course has given me a revived interest in wanting to learn how to use Python to bring Automation some day to my employer / customers to secure or standardize their networks.
I really cannot stress enough that this skillset is extremely powerful, and will sell itself for those of you trying to break into the network industry, I wish I had purchased this course 6 months ago but better late than never!
Anyhow, hope this was informative for everyone as I got quite a bit out of it myself, and I will see you all next time around for more Netmiko fun! 🙂
I said can I connect to scratch 3.0
LikeLike
hey! great article well presented
i am at the point also where i am trying to reference a file with multiple variables. basically same idea but lets say IP addresses would be different for each swith, and referenced in a file. Ideal devices also referenced in a file and matching the sequence. device #1 (file x) – gets IP from file “y”, line #1, etc
any idea ? been looking around for a while!
LikeLike