#!/usr/bin/env python3 import sys import os import getopt import json import subprocess # Based upon work done by Patricio Martelo and Benny Eggerstedt in 2015 # Some corrections and enhancements done by Benny in 2020 # Variables to set by customer ssh_priv_key = "/flash/python/omniswitch_private_ssh_key" config_to_backup = "/flash/working/vcboot.cfg" scp_target_user = "benny" scp_host = "shiva.home" # could also be an IP address scp_path = "/home/benny/configs/" # full path or relative to ~ of the user, include the "/" at the end! # Get system name system_name = os.uname()[1].replace(" ", "_") # Uncomment during early stage of development #print(sys.argv) # Load the data that is being sent to us # -t holds the traptype # -d holds the trapdata try: opts, args = getopt.getopt(sys.argv[1:], "t:d:") except getopt.GetoptError as err: print(err) print("{0}".format(sys.argv)) sys.exit(2) traptype = "(none)" trapdata = "{}" # Go through the data in opts and allocate it properly # traptype gets the value from -t # trapdata gets the value from -d for o, a in opts: if o == "-t": traptype = a elif o == "-d": trapdata = a #print("traptype: {0}".format(traptype)) #print("trapdata: {0}".format(trapdata)) #print(json.loads(trapdata)) trapdetail = json.loads(trapdata) #print(trapdetail) if trapdetail["chassisTrapsAlertNumber"] == 5: # This type of trap is sent when write memory flash-synchro finished #print("detected write memory flash synchro") # When the "write memory flash-synchro" was successful, export the configuration if "success" in trapdetail["chassisTrapsAlertDescr"]: system_date = subprocess.check_output(["system", "date"]).decode("ascii").strip().replace("/", "_") system_time = subprocess.check_output(["system", "time"]).decode("ascii").strip().replace(":", "_") timestamp = system_date + "_" + system_time os.system("scp -v -i {0} -o StrictHostKeyChecking=no {1} {2}@{3}:{4}{5}_{6}.cfg".format(ssh_priv_key, config_to_backup, scp_target_user, scp_host, scp_path, system_name, timestamp)) print("Process finished!")