Python3 Network Programming – Creating a script to Automate getting backup configs from multiple network devices using my previous Topology!

MultiSwitchAuto1

This will be a shorter demo, using the same Topology from the previous lab, to demonstrate writing a script for the NetAuto Host to telnet to switches to backup the running configs and in this demo writing them to the local NetAuto Host.

This will be a pretty quick lab as the steps for using the same template of code and just adjusting some lines is so easy that there isn’t a whole lot to demonstrate, which is part of what makes Python Automation so incredibly powerful for Network Infrastructure.

Jumping right into it, below is the first the raw script text then VSC screen snip

I am just recycling most of the script from my previous command, made a few tweaks I will review below, but most of it will remain the same.

This is the raw text from the script that can be used in your own Python Lab for testing / adding configurations to work with deploying automated backups:

import getpass
import telnetlib
HOST = “localhost”
user = input(“Enter your remote account: “)
password = getpass.getpass()
f = open (‘myswitches’)
for IP in f:
    IP=IP.strip()
    print (“Getting backup config from Switch ” + (IP))
    HOST = IP
    tn = telnetlib.Telnet(HOST)
    tn.read_until(b”Username: “)
    tn.write(user.encode(‘ascii’) + b”\n”)
    if password:
        tn.read_until(b”Password: “)
        tn.write(password.encode(‘ascii’) + b”\n”)
    tn.write(b”term len 0\n”)
    tn.write(b”sh run\n”)
    tn.write(b”wr\n”)
    tn.write(b”exit\n”)
    readoutput = tn.read_all()
    saveoutput = open(“switch” + HOST, “w”)
    saveoutput.write(readoutput.decode(‘ascii’))
    saveoutput.write(“\n”)
    saveoutput.close
And below is the Screen Snip from Visual Studio Code:
Backup1
The changes I made from the previous script:
  1. Line 12 I changed the output on the NetAuto Host to print “Getting config …” rather than “Configuring Switch ” to make more sense
  2. Line 20 – 23 only need User Priv mode to perform the task, I use “terminal length 0” so the “show run” comes out as one solid block of output, then I issue a “write” to save the config before exiting
  3. I removed the print statement at the end, as I will be able to observe the results in the local files that will be created from this script.

Side note – I did a “wr” at the end of my script before exiting the telnet session, as I feel that when taking snapshot backups should always be written after grabbing the config, however if you are troubleshooting or adding configs you will not want to “wr mem” until you are sure your configs work as expected in a Production Network!

For the last set of lines shown:

Lines 25 reads the config, Line 26 opens a file on the NetAuto Host named “switch#” with the HOST indicating the IP Address of the switch, and “w” indicates it open the file as a read/write file as we are writing the output to this file.

Line 27 then writes the output from Line 25’s tn.read_all() that it gathered to the file, Line 28 then performs the equivalent of pressing return in the file, and Line 29 then finally saves the file and closes it.

Now to see this in action on the NetworkAutomation Host!

Backup2

This shows the following happening:

  • I copy / paste my code from VSC to “AutoBackup.py” on host
  • I use “ls” to ensure I have my IP list file
  • I execute the AutoBackup.py script and you see 3 prints as it hits devices
  • I then do another “ls” and we now have 3 new files in the directory

Just to demonstrate the backup is successful I do “cat …” on one of them:

Backup3

I will save all the output, but this demonstrates that “cat switch192.168.238.11” opens the backup config that was fetched and saved via automation, and we now have backups of our networks with the 5 minutes of adjusting a script used for configuring the network 🙂

This does not just pertain to backing up configurations, but also for reviewing them

In my case use I put a “wr” in there to perform a backup of the switch, but if there is a problem in the network, you could use this script to simply telnet to all your devices and grab the current running configs to review for troubleshooting the network.

Python is just so incredibly powerful for usage in network infrastructure it is ridiculous, I cannot believe it took me this long to really dig into it, and so glad that I have!

While Telnet is fine for lab Environments, Production networks require SSH!

The demonstrations I’ve been running through using Telnet are fine for Lab environments, but production networks should absolutely be SSH ONLY for connections, so I will be diving into “Netmiko” next which utilizes SSH to touch Network Devices!

Stay tuned, and keep on the GRIND! 🙂

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