iptables-deploy/deploy.py

53 lines
1.8 KiB
Python

import paramiko
import json
import os
import time # i need time to get this done
# 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
# 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 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"],key_filename=cfg["ssh-key"])
sftp = sshc.open_sftp() # after opening the ssh connection, we'll open a sftp connection.
sftp.put("./payload/payload.sh", "/payload.sh") # upload the payload via SFTP.
sshc.exec_command("chmod +x $HOME/payload.sh") # make it executable
sshc.exec_command("./payload.sh") # and finally, run the payload.
sshc.close # close the connection.
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, deploying payload!")
healthpassing == True;
deploy()
break
else:
print("Got a response! health status is ok.")
time.sleep(30) # we will run this loop every 30 seconds so we don't pelt the poor thing in pings.