Automating Backing Up Router Configuration Using Python Script Made Easy

In Previous articles, we covered about configuration of Cisco devices, and how we access and configure a device. We configured our device from the very beginning, such as how to ping a router or switch, or how to telnet a device through Python Script. Then we ran various commands on routers and configured vlans on a switch. In this article, we will jump to the topic “Automating Backing Up Router Configuration Using Python Script”.

 First, create a text file anywhere on the hard drive. Next, create a Python script and save it beside the text file location that you have created. Write the following script in a Python file. in my case, I have created a text file in the specified location, and a Python file named ‘myfile’ having (.py) extension.

Lab topology for Automating Backing Up Router Configuration

Lab Topology for Automating Backing Up Router Configuration using

We connect the router directly to the cloud (PC). The Router interface fast ethernet 0/0 IP address is 192.168.20.5/24. Set the domain-name of a router. As the netmiko deals in SSH, so configure SSH configuration on a router. Also, enable RSA value, and last set username and password with privilege level 15. The basic configuration of a router is as below;

Basic router configurations

We configured the basic setting of a router. So first we ping the router from cmd, to check its connectivity.

Ping the router

We will save the router configuration in a text file. So first, create an empty text file with the name “textfile” at a location where we will save our Python script. We will save our output configuration in this file.

text file creation

Python Script for automating router backup configuration

In this section, we will write our Python script for Automating Backing Up Router Configuration. The complete Python Script is as below;

Python Script for Automating backing up router configuration

As we have discussed up to line No. 15 regularly, in the previous articles. So, let’s start from line No.17, it will open a text file, at the specified location. ‘a’ means to append a text. In the next line of 19, there is a list of commands. Line No.21 will send the list of commands to the device and will retrieve output from the device. Line No.23 will send the output to the text file ‘mytext’. At the last myfile.close(), will close the text file.

Save the Python script at the location where we created a text file. I saved my file with the name ‘myfile’, having (.py) extension as ‘myfile.py’.

            The script took the username and password and ran successfully. It displayed a message that ‘SSH connection has been established’ with the IP 192.168.20.5

Run the Script

Check the output in a text file. So, open the text file and check any output.

router configuration output in text file

Backup of Multiple Router Configuration Through Python Script

Suppose if we have more than one device, as shown in the below screenshot;

Backing up multiple router configuration

Then we can add the IP addresses of those devices in the list as shown in the below Script. There is no change in the remaining script. Only IP addresses to the ‘Machines’ are added as below:

Machines = [‘192.168.20.5’,’192.168.20.8’,’192.168.20.17’]

Python Script for backing up multiple router configuration

Run the Script again.

run the script

We have shown the text output of the script in the text file. In this way, we automate Backing Up Router Configuration with Python Script.

multiple router backup in text file

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *