major rewrite

This commit is contained in:
2024-02-20 23:24:17 -06:00
parent 9f73b02517
commit 75b143a06a
5 changed files with 39 additions and 111 deletions

View File

@@ -1,7 +1,8 @@
import paramiko
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
@@ -11,7 +12,8 @@ with open("conf.json", "r") as file:
sshc = paramiko.client.SSHClient()
healthstatus = "" # possible values: "ok", "err"
healthpassing = True
healthpassing = True
shcommands = []
# set our variables
def healthcheck():
@@ -22,16 +24,32 @@ def healthcheck():
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"],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.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()
@@ -43,10 +61,10 @@ while True:
healthcheck()
if healthstatus == "ok":
print("ONT responded, deploying payload!")
healthpassing == True;
print("ONT responded after a fail, 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.
time.sleep(cfg["chkinterval"]) # we will run this loop every X seconds, defined by checkinterval