USD ($)
$
United States Dollar
India Rupee

Automate Save Config & Run Backup Script

Created by Ravish Rathi in Articles 15 Jun 2024
Share
«Router on a Stick Configuration Scenario

Tasks

Make a script to open a file with consisting of device’s inventory and add loopback 10,11,12 into them.

Configure 50 loopbacks on all of the devices starting from 1 to 50 and assign ip as 50.1.1.x/32, where x starts from 1 to 50 respectively.

To store all devices backup for running configuration in different files.

To get uptime and device backup without issuing username/password.

Explanation

We will not hard cord devices IPs this time, although making a script to receive it from inventory file saved externally in txt format.

By accessing these IPs it is feasible to run desired commands accordingly.

We will save files containing backup as and when required.

Configuration and Verification

Task1# below script is to run the code for telnet ting to devices mentioned into the file named as Device_Inventory in txt format; script will configure the loopbacks – which are manually mentioned.

import getpass

import telnetlib

user = input(“Enter your username: “)

password = getpass.getpass()

Var_file = open (‘Device_Inventory.txt’)

for VAR_iterate in Var_file:

IP=VAR_iterate.strip()

print (“Configuring Switch ” + (IP))

tn = telnetlib.Telnet(IP)

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”conf t\n”)

tn.write(b”int loop 10\n”)

tn.write(b”description Python_Loop_10\n”)

tn.write(b”exit\n”)

tn.write(b”int loop 11\n”)

tn.write(b”description Python_Loop_11\n”)

tn.write(b”exit\n”)

tn.write(b”int loop 12\n”)

tn.write(b”description Python_Loop_12\n”)

tn.write(b”exit\n”)

tn.write(b”end\n”)

tn.write(b”exit\n”)

print(tn.read_all().decode(‘ascii’))

Task2# Below script is to run the code for telnetting to devices mentioned into the file named as Device_Inventory in txt format; script will configure the loopbacks – which are createdby script using nested loops i.e. FOR under FOR.

import getpass

import telnetlib

user = input(“Enter your username: “)

password = getpass.getpass()

Var_file = open (‘Device_Inventory.txt’)

for VAR_iterate in Var_file:

IP=VAR_iterate.strip()

print (“Configuring Switch ” + (IP))

tn = telnetlib.Telnet(IP)

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”conf t\n”)

for VAR_iterate1 in range (1,52):

tn.write(b”int loop ” + str(VAR_iterate1).encode(‘ascii’) + b”\n”)

tn.write(b”description Python_Loopback_” + str(VAR_iterate1).encode(‘ascii’) + b”\n”)

tn.write(b”ip address 50.1.1.” + str(VAR_iterate1).encode(‘ascii’) + b” 255.255.255.255″ + b”\n”)

tn.write(b”end\n”)

tn.write(b”wr\n”)

tn.write(b”exit\n”)

print(tn.read_all().decode(‘ascii’))

Task3# Below script is to run the code for telnetting to devices mentioned into the file named as Device_Inventory in txt format; script will save running configuration into different files with having mentioned IPs in file name.

import getpass

import telnetlib

user = input(“Enter your username: “)

password = getpass.getpass()

Var_file = open (‘Device_Inventory.txt’)

for VAR_iterate in Var_file:

IP=VAR_iterate.strip()

print (“Configuring Switch ” + (IP))

tn = telnetlib.Telnet(IP)

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”terminal length 0\n”)

tn.write(b”show run\n”)

tn.write(b’exit\n’)

readoutput = tn.read_all()

saveoutput =  open(“Device_Backup_IP” + IP + “.txt”, “w”)

saveoutput.write(readoutput.decode(‘ascii’))

saveoutput.write(“\n”)

saveoutput.close

Task4# Below script is to run the code for telnetting to devices mentioned into the file named as Device_Inventory in txt format; script will save running configuration, uptime information into different files with having mentioned IPs in file name. Also we are hardcoding username and password – In future we can save this in BAT format.

import getpass

import telnetlib

user = “cisco”

password = “cisco”

Var_file = open (‘Device_Inventory.txt’)

for Var_iterate in Var_file:

IP=Var_iterate.strip()

print (“Entering Device For Uptime ” + (IP))

tn = telnetlib.Telnet(IP)

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”show ver | i uptime\n”)

tn.write(b”exit\n”)

readoutput = tn.read_all()

print (“Entering Device For Running Configuration ” + (IP))

tn = telnetlib.Telnet(IP)

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”terminal length 0\n”)

tn.write(b”show run\n”)

tn.write(b”exit\n”)

readoutput1 = tn.read_all()

saveoutput =  open(“Device_Backup_IP_” + IP + “.txt”, “w”)

saveoutput.write(“**Here is the Device Uptime Information**”)

saveoutput.write(readoutput.decode(‘ascii’))

saveoutput.write(“\n”)

saveoutput.write(“**Here is the Configuration Backup**”)

saveoutput.write(readoutput1.decode(‘ascii’))

saveoutput.write(“\n”)

saveoutput.close

Output is shown as follows –


Ravish Rathi

Ravish Rathi is a currently working as a Senior Network Consultant with one of the world's largest Internet Service Provider. He started his career as network support engineer with HCL and since than he has been working on different roles with various organizations such as Accenture, IBM, HCL, HP etc. Now he is having more than 15 years of ...

More... | Author`s Bog | Book a Meeting

Related Articles

#Explore latest news and articles

How to Configure a Banner in Cisco Router 15 Jun 2024

How to Configure a Banner in Cisco Router

Learn how to configure a banner in a Cisco router with our step-by-step guide. Set MoTD banners on Cisco routers and switches. 
Difference between Router-Switch: An Insight 11 Jun 2024

Difference between Router-Switch: An Insight

Get insights into what is router and what is network switch and understand the difference between Router Switch.

Comments (0)

Ravish Rathi

Ravish Rathi

Senior Network Consultant and Instructor Instructor role
★★★★★ 5.00
Faithful User
Expert Vendor
Golden Classes
King Seller
Fantastic Support

Upcoming batches

+91 84484 40748

Enquire Now

Share to your friends

Share

Share this post with others

Contact learning advisor

New Cisco ACI Training Live Batch Starts June 15th!
New Cisco ACI Training Live Batch Starts June 15th!
Advance your career? Don't miss out on our Cisco Application Centric Infrastructure (ACI) training ...
Enroll Now