Skip to content

Instantly share code, notes, and snippets.

@pkdiv
Created September 24, 2025 18:02
Show Gist options
  • Select an option

  • Save pkdiv/24769806b4675363f389a2594a0b7fd9 to your computer and use it in GitHub Desktop.

Select an option

Save pkdiv/24769806b4675363f389a2594a0b7fd9 to your computer and use it in GitHub Desktop.
Versa Device Backup script
#!/usr/bin/env python3
import paramiko
import time
import socket
import os
backup_path = "/usr/local/rancid/var/SDWAN_backups/"
def device_backup(hostname, username, password, command, timeout):
try:
ssh_client = paramiko.client.SSHClient()
ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ip_address = resolve_hostname(hostname)
print(f"{hostname} resolves to {ip_address}")
ssh_client.connect(ip_address, username=username, password=password, port='22')
shell = ssh_client.invoke_shell()
print(f"Connected to {hostname}")
time.sleep(2)
shell.send("cli\n")
time.sleep(5)
command = "show configuration | display set | nomore"
shell.send(command + "\n")
print(f"Executing '{command}' and gathering output")
time.sleep(timeout)
shell.send('exit')
output = ""
last_output = ""
start_time = time.time()
while True:
chunk = shell.recv(65535).decode('utf-8')
output += chunk
if "[ok]" in chunk:
break
print("Command output received")
ssh_client.close()
return output
except Exception as e:
return f"{e}"
def write_to_file(filename, data):
print(f"Writing to file {filename}")
with open(backup_path + filename, "w") as file:
file.write(data)
def resolve_hostname(hostname):
try:
ip_address = socket.gethostbyname(hostname)
return ip_address
except socket.error as err:
return False
def initate_backups(device_list_file):
with open(device_list_file, 'r') as file:
for hostname in file:
hostname = hostname.strip()
backup_data = device_backup(hostname, 'admin', 'Ep!m4rmSd0f$5e' ,"show configuration | display set | nomore\n", 50)
write_to_file(hostname, backup_data)
if __name__ == '__main__':
initate_backups('/usr/local/rancid/scripts/sdwan_list.txt')
time.sleep(1200)
os.chdir(backup_path)
os.system('git add .')
os.system('git commit -m update')
os.system('git push origin')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment