Python to Backup Cisco Configuration
Here is an example of how to write a Python script to backup the configuration of a Cisco device using the
paramiko library, which is a Python implementation of the SSH protocol:
# create an SSH client object
ssh = paramiko.SSHClient()
# add the Cisco device’s host key to the SSH client’s list of known hosts
# connect to the Cisco device
ssh.connect(hostname=’cisco-device’, username=’cisco’, password=’cisco’)
# run the ‘show run’ command and save the output to a variable
stdin, stdout, stderr = ssh.exec_command(‘show run’)
output = stdout.read().decode()
# write the output to a file
with open(‘cisco-config-backup.txt’, ‘w’) as f:
# close the SSH connection
This script connects to a Cisco device with the hostname ‘cisco-device’, username ‘cisco’ and password ‘cisco’ using SSH. It then runs the command ‘show run’ to retrieve the current configuration of the device. The output of this command is saved to a variable, and then written to a file named ‘cisco-config-backup.txt’. Finally, the script closes the SSH connection.
You might also add a timestamp to the backup file name and also handle errors if can’t connect to the device or something went wrong, like file write.