Home > Others > A handy script to backup configurations on multiple network devices.

A handy script to backup configurations on multiple network devices.

If you don’t have a network configuration manager in your environment, this small python script will do the job for you. It uses Python Netmiko library which allows you to ssh into network devices and execut commands without installing anything on the end client.

Let’s review the script.

This is where we import libraries for our script. We will use the datetime module for knowing the current day so we can use it to name the file.

from netmiko import ConnectHandler
import datetime

now = datetime.datetime.now()

Next step is defining username, password and device IPs that will be used when the script is logging into devices. We will also use the show run” command to print all the current configurations on arista and cisco devices. If you are planning to use this script for another brand, you can customize the command. For example, for Juniper it would be “show configuration.”

#login info, IPs and commands can be adjusted as needed. 

username = "test"
password = "test"
command = "sh run"
#A new list can be added for the IPs of different device types
sw_arista = ['70.70.70.2', '70.70.70.3']
sw_nexus =['70.70.70.1']
sw_juniper =[] 

A for loop will be more than enough to go through each IP in the list, execute the command and save the configuration to a file and name the file after the ip of the device and name date of the day. Configs will be saved to the same directory as the script.

for nx_ip in sw_nexus:
#netmiko will log in with below info and execute the command.
    print ('connecting ' + nx_ip)
    nexus_login = {
    'device_type': 'cisco_nxos',
    'ip': nx_ip,
    'username': username,
    'password': password,
    }
    nx_connect = ConnectHandler(**nexus_login)
    output = nx_connect.send_command(command)
#file name is defined to save the output
    name = nx_ip + "_" + str(now.month) + "_" + str(now.day) + "_" + str(now.year) + ".txt"
#open write and close the file. 
    file = open(name,"w")
    file.write(output)
    file.close()
    print ("congifs are backed up on in desktop folder")

This will do the job for Arista.

for ar_ip in sw_arista:
    print ('connecting ' + ar_ip + '\n')
    arista_login = {
    'device_type': 'arista_eos',
    'ip': ar_ip,
    'username': username,
    'password': password,
    }
    ar_connect = ConnectHandler(**arista_login)
    output = ar_connect.send_command(command)
    name = ar_ip + "_" + str(now.month) + "_" + str(now.day) + "_" + str(now.year) + ".txt"
    file = open(name,"w")
    file.write(output)
    file.close()
    print ("congifs are backed up on in desktop folder")

You can keep using the same for loop as you need to add different device types but make sure to change device type and IPs. Below are a couple of other examples of how you can define hosts, device types, and IPs. If you are using hostnames instead of IPs, make sure device names are in the DNS so you don’t get a connection error.

cisco3 = {
   ...:     'device_type': 'cisco_ios',
   ...:     'host':   'cisco3.domain.com',
   ...:     'username': 'admin',
   ...:     'password': 'cisco123',
   ...: }
   ...:

cisco_asa = {
   ...:     'device_type': 'cisco_asa',
   ...:     'host': '10.10.10.88',
   ...:     'username': 'admin',
   ...:     'password': 'cisco123',
   ...:     'secret': 'cisco123',
   ...: }
   ...:

cisco_xrv = {
   ...:     'device_type': 'cisco_xr', 
   ...:     'host': '10.10.10.77', 
   ...:     'username': 'admin', 
   ...:     'password': 'cisco123', 
   ...: }

arista8 = { 
   ...:     'device_type': 'arista_eos', 
   ...:     'host':   'arista8.domain.com', 
   ...:     'username': 'admin', 
   ...:     'password': '!cisco123', 
   ...: } 
   ...:

hp_procurve = { 
   ...:     'device_type': 'hp_procurve', 
   ...:     'host': '10.10.10.68', 
   ...:     'username': 'admin', 
   ...:     'password': '!cisco123', 
   ...: } 
   ...:

juniper_srx = { 
   ...:     'device_type': 'juniper', 
   ...:     'host':   'srx1.domain.com', 
   ...:     'username': 'admin', 
   ...:     'password': '!cisco123', 
   ...: } 

Here is the full script:

#!/usr/bin/env python
#never name a file after a library. Your code will not work

from netmiko import ConnectHandler
import datetime

now = datetime.datetime.now()

username = input("what is the username: ")
password = input("what is the password: ")
command = "sh run"

sw_arista = ['70.70.70.2', '70.70.70.3']
sw_nexus =['70.70.70.1']
sw_juniper =[] 

for nx_ip in sw_nexus:
    print ('connecting ' + nx_ip)
    nexus_login = {
    'device_type': 'cisco_nxos',
    'ip': nx_ip,
    'username': username,
    'password': password,
    }
    nx_connect = ConnectHandler(**nexus_login)
    output = nx_connect.send_command(command)
    name = nx_ip + "_" + str(now.month) + "_" + str(now.day) + "_" + str(now.year) + ".txt"
    file = open(name,"w")
    file.write(output)
    file.close()
    print ("congifs are backed up on in desktop folder")


for ar_ip in sw_arista:
    print ('connecting ' + ar_ip + '\n')
    arista_login = {
    'device_type': 'arista_eos',
    'ip': ar_ip,
    'username': username,
    'password': password,
    }
    ar_connect = ConnectHandler(**arista_login)
    output = ar_connect.send_command(command)
    name = ar_ip + "_" + str(now.month) + "_" + str(now.day) + "_" + str(now.year) + ".txt"
    file = open(name,"w")
    file.write(output)
    file.close()
    print ("congifs are backed up on in desktop folder")

How do we run this script automatically at a specific time/day of the week.

Windows, task scheduler can be used to run scripts. If is fairly simple to set it up. On Linux, tasks/ scripts can be scheduled to run automatically at specific times by using the command line.

There are a few improvements that can be made to this script.

  • We can define a function to combine all the for loops under the same function.
  • We can get device names and configs from a file and just update the device list file when you need to add new devices so there is no need to keep editing the script.
  • Usernames, device IPs, and backup configs can be encrypted by another python library if security is a concern.

Please let me know in comments how are you managing configuration backups.

Feel free to suggest if there are any other improvements that can be made to this script.

Leave a Reply

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