This is history in the making – My first ever Python network configuration!!!
As it appears, it is only logging into R1 via Telnet to add an interface lo0, then adding an IP Address of 126.96.36.199/32 and writing mem / exiting the router and it worked!
I start with the Telnet Example that can be found here in the Python Telnet Library page which as an example start out as this:
import getpass import telnetlib HOST = "localhost" user = input("Enter your remote account: ") password = getpass.getpass() tn = telnetlib.Telnet(HOST) tn.read_until(b"login: ") tn.write(user.encode('ascii') + b"\n") if password: tn.read_until(b"Password: ") tn.write(password.encode('ascii') + b"\n") tn.write(b"ls\n") tn.write(b"exit\n") print(tn.read_all().decode('ascii'))
The part of this script highlighted in Blue are imported libraries, which as I think was one of my last posts (if I published it) explains how you will import different libraries at the beginning of scripts to use their functions / variables / etc.
The part of this script highlighted in Red are values that needed to be changed, as the Host I wanted to reach was R1 (not the localhost), the login prompt is not “login: ” so it would not trigger the username I input to be used in the script, and finally the “tn.write(b”anythingyouwanttoinput)\n)” will input to the console once it inputs the username and password and you are now essentially logged in!
So in the default example, “ls” would “list” the items in the current directory, so this script by default if run exactly as is in docker would display the directory items and exit (assuming its being run in Linux that understands “ls”).
Before getting into Automation Config Examples, a quick look at the Topology
There are some important details to this that as a GNS3 beginner (I’ve been spoiled to have a mostly relevant Physical Lab for almost a decade), and I couldn’t get my VIRL images working in EVE-NG, I had to surrender to GNS3 as it actually comes with many built in features that are pretty amazing once I dug in.
Anyhow, I started with the Topology below, which I will list items and setup below:
1 x “NetworkAutomation” host that can run Python 2.x or 3.x
1 x generic L2 switch to connect Automate host to NAT Cloud and VIRL devices
1 x VIRL L2 Multi-Layer Switch
1 x VIRL AdvEnterprisek9 15.6 Router
1 x NAT Cloud to Bridge the Virtual Network to my home network
I also tweaked the GNS3 VM Network Settings to “Bridge” to my home network, so while this virtual LAN is completely separate (not even seen by my Meraki network), I can ping devices from my local PC and can ping from virtual devices to the internet.
This is a good idea also so you can run “get-apt update” on the Linux Host to update it.
This Topology will grow but a small starter network to test the automation waters 🙂
With Topology covered, I slapped the Python Telnet Template in Visual Studio Code
At this time I am not aware of any sort of “zero touch” provisioning like some magical SD-WAN or DNA-Center level stuff, so I setup the following for the NetworkAuto Host device to be able to Telnet to the Router (R1) first:
- “username looped password back”
- “enable secret loopedback”
- “ip add 192.168.238.125 255.255.255.0” on int Gi0/0, “no shut”
- “line vty 0 5” / “login local” / “transport input all”
With both switches running at Layer 2 and NetworkAuto Host running DHCP, I confirmed connectivity then altered the default template to Telnet to R1 and configure a single loopback interface on it.
Here is my script in Visual Studio Code with #explanations of lines included in green:
Being that I am not quite sure how to call out to my Local PC at this time I copied this code from VSC (minus the #comments) and ctrl+c to copy, however on the NetworkAuto Host I first issued “nano TelnetR1.py” to create a .py file in the directory and right clicked to paste as with any Telnet / SSH session:
Its a bit clunky for me not being overly familiar with Linux, if I had to start over I honestly did a “ctrl+x” to exit back, then “rm -f TelnetR1.py” to just ditch the file, and re-create the file with and re-paste in the new code if I had to make changes.
There are probably easier ways to accomplish this, though I am not there yet 🙂
As can be seen in the screen snip (and explained below) I learned a few things:
This taught me multiple lessons all in a very short amount of time 🙂
- This Linux machine had no directory that contained the VSC script I created, so it popped out an error instead of running it
- There was no items in this direction because I had not created anything, so “ls” returned nothing when I checked the directory for items
- That I was initially missing a “config t” to put myself in Global Config for this script!
That is the beauty of these lines once you get your Telnet IP / creds connected, you can just input an entire configuration to build a router via Python:
Once it connects it is in “User Exec” mode, which is why the two tn.write lines are there for “enable” and “loopedback” as the enable secret, and from there I hit “conf t” and I am now in configuration mode and can just cope / paste that tn.write line a million times and fill it in like how I would make baseline Router config templates for my physical lab!
Then of course once I was completed with inputting this and writing it to the NetworkAutomation Host, “ls” now shows “TelnetR1.py” in the directory, and when I type “python3 TelnetR1.py” it will ask my for a username and password for initial connection then add whatever I put in beyond the enable and enable secret.
It shows the username “looped” on vty0 from the NetworkAutomation Host @ 192.168.238.153 create interface Loopback0 but does not show the IP Address being applied, so lets verify that in fact got applied:
It sure did!
One thing I contemplated at first was if the immediate write / exit would break the writing to memory if the telnet session ended immediately without a wait in there, but it continued to write mem despite the telnet session ending as the Router is still running and doesn’t need the telnet session that made the commit / write mem connected to complete saving the configuration.
After this I decided to try my hand at a quick VIRL Switch Auto-config as well
Being that the switch requires the same things like a MGMT IP Address (Assigned to VLAN 1), and the username/password/enable secret/login local/transport input all.
Here is the code from Visual Studio Code to “nano TelnetSW1.py” :
So a little more here, added an extra VLAN with Description and a Loopback, fancy! :
And then when executing the Python script within the NetworkAuto Host :
So that “Print” statement at the end of the script kicks out what is executed on the device being configured, so you can see if errors are abound, like when I goofed putting in a conf t on the original R1 automate script.
As seen by the “ls” at the end I also remove the TelnetR1.py with the “rm -f filename.py” command as I plan to expand upon that initial very simply loopback interface script 🙂
That is all I have for now, I hope this was helpful to those looking to get started!
I highly recommend going to Udemy, and investing the $40 in David Bombals class, as I am barely into it and already I have a solid grasp of at least how to automate networks within GNS3 and I’ve only watched 2 videos outside of the GNS3 setup videos 🙂
The feeling of grasping how to automate network configs at all is an amazing feeling just getting back into the grind, I will start cranking out some more good stuff soon!
Happy Friday! 🙂