USD ($)
$
United States Dollar
India Rupee

How to Configure Backup Script and Autosave in Cisco Routers

Created by Ravish Rathi in Articles 12 Sep 2024
Share
«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
custom banner static image

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-

banner image


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 30 Oct 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 login, exec, and MoTD banners on Cisco routers and switches. 
What is the Difference between Router and Switch 11 Nov 2024

What is the Difference between Router and Switch

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

FAQ

A backup script in Cisco routers automates the process of saving the device's configuration files to a secure location, such as an FTP or SFTP server. This ensures that configurations can be quickly restored in case of device failure or misconfiguration
You can schedule automatic backups using tools like Python with libraries such as Netmiko or Paramiko. By writing a script that connects to the router and retrieves the configuration, you can use scheduling modules to run the script at specified intervals, ensuring regular backups without manual intervention.
Using autosave for router configurations provides several benefits, including enhanced security through regular backups, reduced downtime during failures, and easier recovery of configurations. It also minimizes the risk of human error by automating the backup process.
Yes, you can use Telnet to back up Cisco router configurations. However, it is recommended to use SSH instead, as it provides a more secure connection. Scripts can be written to automate the Telnet process, allowing for efficient configuration backups.

Comments (0)

Share

Share this post with others

Contact learning advisor

Captcha image
Cisco SD-WAN Training – Starts Nov 23rd at 10 AM IST
Cisco SD-WAN Training – Starts Nov 23rd at 10 AM IST
Boost Your Networking Career with Expert-Led Cisco SD-WAN Training with Lab Access.
Day
Hr
Min
Sec
Register Now