Python3 Network Programming – Creating a very simple script that automates unlimited devices, nesting loops within loops, and labs to demo how absolutely powerful Network Automation is!


I will be automating the three switches in the above Topology with a single script!

But first an important note before reading:

This simple demo took all of an hour or so to lab up and post here, Network Automation with Python is SUCH a powerful skillset to have at your disposal, it is ridiculous how powerful Python can be for networks which I never knew before I bought David Bombals $40 Udemy course “Python for Network Engineers” but now I can’t believe I have waited this long to pull the trigger on buying it!

This skillset sells itself, imagine white boarding a script that calls out to every network device / backs up the config / tftp’s it offsite or writes it to a text file during an interview for a simple “Network Admin” job, I would tell the candidate they are hired on the spot!

I lab and post it here for my own notes and to help guide people in the right direction, but David Bombal is just a master of teaching it, I am not being paid to say that you are doing yourself a huge disservice if you are new to Python / Automation!


Now that I got that out, a quick description of what I will be doing in this demo!

This lab will demonstrate first creating a file that contains a list of Host IP’s of the switches within the Topology, and configuring a couple static VLANs, and move onto nesting a range loop within the existing loop to then create VLANs dynamically as demonstrated in the previous post.

Keep in mind these scripts and files must reside on the NetworkAutomation Host in GNS3, however I write the script in Visual Studio Code first, then paste that script into a python file create on the NetAuto Host.

First I will be doing a quick configuration of a couple manually created VLANs, then I will nest a loop within a loop to demo how to add the snippet of code of the “range” loop script that created 10 VLANs automagically to this new script that touches multiples network devices to write configurations!

First to review the script made in Visual Studio Code to hit multiple devices

This is code I wrote in VSC, then created the Python script on the NetworkAutomation Host with “nano” which opens a blank window, then I ctrl+c in VSC to copy the highlighted code and right click into NetAuto Host to paste it.

Visual Studio Code has nice features like highlighting errors, auto indentation, and tons of other things that makes writing code a lot easier than directly on a host device!


So the first difference seen is the Host = “localhost” rather than a remote IP Address, as we will be opening the file from a directory on the NetAuto Host, and later changing the HOST variable down the script.

The next is f = open (‘somefile’) which sets “f” as a variable to open the file name defined within the () parameters, which in this case is a text file that I created using “nano myswitches” without an extension which creates a text file, and input IP addresses:


These are the MGMT IP Addresses for SW1 / SW2 / SW3, I also created a file to test a simple print function to make sure this file was working with “nano” :


Then with a ctrl + o to save then ctrl + x to get back to the root prompt, I can then test this script to make sure my IP’s all appear as expected and the text file is correct:


With that explained I’ll bring the script back up to continue review of it here:


Next we enter a ‘for’ loop which says for every IP in f which equals open the myswitches file, do the following which is indicated of course by the indentation.

The IP=IP.strip() is in there to make sure it read only the IP and no null or extra spaces the file might contain.

Then there is a “print” function that shows in the NetAuto Host terminal which “Configuring Switch IP” when it moves from one IP to the next, but my script is so small you barely see it zip by.

Then HOST is changed to = IP which refers to the IP List, and then the telnet(HOST) line follows immediately after, follow by the username and ‘if’ loop to input the password.

Then from here I just gave the local user “looped” priv 15 so it just right to User Priv mode, allowing it to go directly into Global Configuration and manually right 2 VLANs, write to memory then exit the telnet session.

Finally we have the same print statement at the end to return the output from the switch to the NetAuto Host running the script, so lets give it a try to demo this simple script:


I’ll spare the output from every switch, but as can be seen here, all 3 switches now contain the following in their “sh vlan brief” output:


I only made two VLANs for the sake of a demo, however within that script once we hit config mode we could fill it up to turn on all sorts of features on a switch:

  • Port Security
  • QoS
  • Storm Control
  • Interface Descriptions
  • IP Routing / Dynamic Routing Protocols
  • And on and on

That is really the power of Automation and how it can standardize a network, there are probably more slick ways to do it with Ansible which I’ve yet to touch, but with Python you can use a template like this to standardize every device in the network.

However what if we want to create 20 VLANs on multiple switches?

There is a loop for that! A “for” loop specifically šŸ™‚

I will take the snip from my previous for / range loop, and apply it to this script so that it creates VLANs 3-20 on each switch automatically by adding to the existing script.

I remove all the manual VLAN Configuration in the script and replace it with the loop snippet that I have in my previous script (I always save script separately for this reason):


This is referred to as “nesting loops” as this for loop is part of the parent “for” loop of connecting to this list of devices defined by the IP Address list, I am not sure if it needs that space between tn.write lines but I put it in there just in case, and just like that:



I cannot stress that enough, even if you aren’t going for the DevNet certs, you should run these kind of labs with different configurations and see how you can bend Python to your will to fully configure networks!

I am admittedly still very much in my infancy of working with Python, but this skillset just sells itself to any employer that knows how to calculate the cost of network engineer labor time in configuring 20-50 devices.

You can add lines to the script to write additional configs to “wr mem”, you can add new IP’s to the list for the script to write to, and this is literally just the very tip of the ice berg of what can be done with Python and network automation.

Network Automation with Python is a skill you cannot afford not to learn

I just cannot say that enough after taking this course for a bit, the Python language is starting to soak in a lot more because I am using it in the realm I know of “Networking” as just writing “Hello World!” code got old fast.

If you are a Network Engineer, I hope you join me here to follow along (though I will soon be breaking off into DevNet Blueprint topics), but I really cannot say it enough that the $40 investment in David Bombals course on Udemy is the best investment you will make in your career up to this point if you are getting into the Network field!

That is all my ranting about Automation for today, see ya! šŸ™‚



Leave a Reply

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

You are commenting using your 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