import paramiko # this sounds like an anime import json import os import time # i need time to get this done import requests # for getting the commands to run on client # import modules with open("conf.json", "r") as file: cfg = json.load(file) # open the config file and make it accessible via "cfg" sshc = paramiko.client.SSHClient() healthstatus = "" # possible values: "ok", "err" healthpassing = True shcommands = [] # set our variables def healthcheck(): global healthstatus response = os.system("ping -c 2 " + cfg["server"] + ">> /dev/null") if response != 0: healthstatus = "err" else: healthstatus = "ok" def downloadLatestCommands(url): r = requests.get(f'{url}') open(cfg["filepath"], 'wb').write(r.content) def commandList(): global shcommands cmdtxt = open(cfg["filepath"], "r") cmddata = cmdtxt.read() shcommands = cmddata.split("\n") cmdtxt.close() def deploy(): sshc.set_missing_host_key_policy(paramiko.AutoAddPolicy()) # so we don't get whined at and crash over a unrecognized host-key sshc.connect(cfg["server"],port=cfg["port"],username=cfg["ssh-user"],password=cfg["ssh-pw"],pkey=cfg["ssh-key"]) for command in shcommands: sshc.exec_command(command) print(f'{command} was executed') print("All commands were executed, now disconnecting...") sshc.close # close the connection. # sshc.exec_command("chmod +x $HOME/payload.sh") # make it executable # sshc.exec_command("./payload.sh") # and finally, run the payload. while True: healthcheck() if healthstatus != "ok": print("ONT is not responding!! Did we lose network connection, or is the ONT rebooting? waiting for ONT to respond, then deploying payload!") healthpassing = False while healthpassing == False: print("Checking for a response...") healthcheck() if healthstatus == "ok": print("ONT responded after a fail, deploying payload!") healthpassing == True deploy() break else: print("Got a response! health status is ok.") time.sleep(cfg["chkinterval"]) # we will run this loop every X seconds, defined by checkinterval