Netmiko Backup Cisco

Overview

This mini project spawned from the fact that I wanted to backup my Netmiko TACACS lab,and to have something reusable that a quick modification to one or two files (not the script) would result in config backups for my labs. I wrote a short post that resurrected a basic backup script.

This mini project uses that as a foundation, but removes the need for the user to enter any passwords and the use of multi threading.

In fact I compared the times of running this script with a single thread to using 8 threads for 11 devices (4 of which had failures).
As assumed the multi threaded was much quicker. 10 seconds to 28 seconds.

I needed to modify the print outputs for this compared with the original linear script. To do this I created a dictionary when running each thread that contained the hostname, IP and file name, then added that to a list that printed out at the end.

All of the code for this project may be found on my GitHub.

Revision1 is the latest version.

Netmiko Lab

Print Output for Multi Threading

Printing the output in a neat way when using multi threading. This waits until the end of the script to print out what is successful and what wasn’t. An example can be seen the section below this one.

def config(device, backup_list, error_ips):
# Other code, see GitHub

# Dictionary for the Output
            backup_dict = {"IP Address":host_ip, 
                            "Hostname":hostname, 
                            "File Name":file_name} 
            backup_list.append(backup_dict)  
    # Print Backups
    print("\n")
    for device in backup_list:
        print(f"Successfully performed backup on:\t{device['Hostname']}\t{device['IP Address']}\t file: {device['File Name']}")
    print("*"*60)

Multi Threaded Output

$ python backup.py
These are the devices that we will be connecting to...
IP Address: 192.168.122.102
IP Address: 192.168.122.194
IP Address: 192.168.122.98
IP Address: 192.168.122.105
IP Address: 192.168.122.107
IP Address: 192.168.122.104
IP Address: 192.168.122.115
IP Address: 192.168.122.118
IP Address: 192.168.122.119
IP Address: 192.168.122.123
IP Address: 192.168.122.124


SSH connection established to 192.168.122.98:22
SSH connection established to 192.168.122.102:22
Interactive SSH session established
Interactive SSH session established
SSH connection established to 192.168.122.107:22
SSH connection established to 192.168.122.105:22
Interactive SSH session established
Interactive SSH session established
SSH connection established to 192.168.122.119:22
Interactive SSH session established
SSH connection established to 192.168.122.123:22
Interactive SSH session established
SSH connection established to 192.168.122.124:22
Interactive SSH session established
There is an error connecting to 192.168.122.194
Continuing...

There is an error connecting to 192.168.122.104
Continuing...

There is an error connecting to 192.168.122.115
Continuing...

There is an error connecting to 192.168.122.118
Continuing...



************************************************************
Error connecting to 192.168.122.194
Error connecting to 192.168.122.104
Error connecting to 192.168.122.115
Error connecting to 192.168.122.118


Successfully performed backup on:       R4      192.168.122.105  file: R4_2022-9-23_backup.cfg
Successfully performed backup on:       R1      192.168.122.102  file: R1_2022-9-23_backup.cfg
Successfully performed backup on:       R3      192.168.122.98   file: R3_2022-9-23_backup.cfg
Successfully performed backup on:       R5      192.168.122.107  file: R5_2022-9-23_backup.cfg
Successfully performed backup on:       NX6_v7  192.168.122.119  file: NX6_v7_2022-9-23_backup.cfg
Successfully performed backup on:       NX3_v9  192.168.122.123  file: NX3_v9_2022-9-23_backup.cfg
Successfully performed backup on:       NX4_v9  192.168.122.124  file: NX4_v9_2022-9-23_backup.cfg
************************************************************
Execution time in seconds: 10.020684719085693