Dev: pyNAT-Backup.v1
Hi there, this script can be used to backup a group of devices automatically at a specific time. If you plan to use inside your organization, you can scale this configuration by reading the IP addresses from a file. As for the credentials, you should be using an encrypted file instead of hard coding the password on your script.
To schedule the task I used Task Scheduler on Windows.
Worth mentioning that this script may have a different behavior if run Linux.
import os
from netmiko import ConnectHandler
from datetime import datetime
import time, subprocess
print("***Backup party is about to start****")
timeLeft = 5
while timeLeft > 0:
print(timeLeft, end=' \n')
time.sleep(1)
timeLeft = timeLeft - 1
# just for fun, I added this to play a song
subprocess.Popen(['start', 'absolute_filepath\filename.mp3'], shell=True)
time.sleep(2)
for ip in range(1, 5):
router = {
"host": "192.168.234.3{}".format(ip),
"username": "silesio",
"password": "superpassword",
"device_type": "cisco_ios"
}
login_device = ConnectHandler(**router)
cmd = login_device.send_command("show run")
get_hostname = login_device.send_command("show run | in hostname")
get_hostname = get_hostname.split(" ")
hostname = get_hostname[1]
date = datetime.now().strftime("%Y_%m_%d-%I_%M_%S_%p")
filename = hostname + "_" + date + ".txt"
with open(filename, 'w') as f:
f.write(cmd)
time.sleep(1)
print(f"\n{hostname}", "Backup completed!")
print("\nok back to work :-)")
time.sleep(2)
To schedule the task to run automatically, set the Trigger and the Action. Use the absolute path to run your code.
And that’s it.
I hope you enjoyed this post, leave your comments below and I'll see you on the next one.