«Router on a Stick Configuration: ...
Running backup scripts and autosaving configurations in Cisco routers is crucial for maintaining network reliability and security. This process ensures that the current running configuration is preserved, allowing for quick recovery in case of device failure or accidental changes.
Cisco IOS provides commands and features to automate these backups, enabling network administrators to schedule regular saves or trigger backups during specific events. To effectively manage these configurations, Cisco training is necessary.
This article will guide you through the steps to configure backup scripts and enable autosave features, ensuring your router configurations are consistently backed up and easily recoverable.
Tasks
We will be performing these 4 tasks to configure the backup script and autosave configurations.
● Make a script to open a file consisting of the 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' backups for running configuration in different files.
● To get uptime and device backup without issuing a username/password.
Explanation
We will not hard cord device IPs this time, although making a script to receive it from the 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.
CCNA Live TrainingOnline training for CCNA certification.Explore course Task 1: Configuring Loopbacks using Telnet
The below script is to run the code for telnet ting to devices mentioned in the file named Device_Inventory in txt format; the 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’)) |
Task 2: Configuring Loopbacks using Nested Loops
The Below script runs the code for telnetting to devices mentioned in the file named Device_Inventory in txt format; the script will configure the loopbacks – which are created by the 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’)) |
Task 3: Saving Running Configuration to Files
The Below script is to run the code for telnetting to devices mentioned in the file named Device_Inventory in txt format; the script will save the running configuration into different files with having mentioned IPs in the 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 |
Task 4: Saving Running Configuration and Uptime
The Below script is to run the code for telnetting to devices mentioned in the file named Device_Inventory in txt format; the script will save running configuration, and uptime information into different files with having mentioned IPs in the file name. Also, we are hardcoding username and password – In the 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-