#!/usr/bin/env python3 import paramiko from git import Repo import csv import time from config import * #config variables from config.py #Check entries in the devices.csv document and import them into the dictionary devices = [] #dictionary with open(devicescsv_path,'r') as devicelist: list = csv.DictReader(devicelist) for row in list: devices.append(row) #for each entry do an export and download it for device in devices: file_path = repository_path + "/" + device["Location"] + "/" + device["Location"] + "_" + device["DeviceName"] + ".rsc" # Create an SSH client instance ssh_client = paramiko.SSHClient() ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) try: # Connect to the server ssh_client.connect(hostname = device["HostName"], port = device["Port"], username = device["User"], pkey = paramiko.RSAKey.from_private_key_file(key_path)) except Exception as e: print(f"An error occurred: {e}") else: print("Connected to " + device["HostName"]) #Export config stdin, stdout, stderr= ssh_client.exec_command("export show-sensitive") print("Export commanded") with open(file_path, "w") as f: #write export into file f.writelines(stdout.readlines()) print("Export written into file") finally: # Close the SSH connection ssh_client.close() print("Connection closed") # Open exported document, remove date and time of export with open(file_path, 'r') as file: lines = file.readlines() if lines: # Check if the file is not empty lines[0] = lines[0][:2] + lines[0][25:] #remove date and time of export with open(file_path, 'w') as file: file.writelines(lines) repository = Repo(repository_path) if repository.bare: print("The repository is bare") exit() if repository.is_dirty(untracked_files=True): #Check if there are any differences print("Changes detected in the repository.") repository.git.add(all=True) #Stage all changes print("Staged all changes.") commit_message = time.strftime("%Y.%m.%d %H:%M:%S", time.localtime()) #commit changes repository.index.commit(commit_message) print("Committed changes with message:" + commit_message) origin = repository.remote(name='origin') #push origin.push() print("Pushed changes to the remote repository.") else: print("No changes to commit.")