Cisco ACI and CCNA live online batches are starting from 24th June 2023. Join experts today. Automate Saving Configurations Archives - UniNets Blog

Tag Archive Automate Saving Configurations

How Can I Automate Saving Configurations, Running Backup Script for Whole Site

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 –

output image