|
|
|
import requests
|
|
|
|
from wakeonlan import send_magic_packet
|
|
|
|
import time
|
|
|
|
import argparse
|
|
|
|
|
|
|
|
|
|
|
|
def assert_availibilty(target_host: str, wake_if_offline: bool = False):
|
|
|
|
# Ping the host to check if it's online
|
|
|
|
print("Verfügbarkeit des Ziel-Hostes wird überprüft.")
|
|
|
|
ips = get_target_ips()
|
|
|
|
ip_target = ips[target_host]
|
|
|
|
target_node_route = f'http://{ip_target}:9713/status'
|
|
|
|
try:
|
|
|
|
response = requests.get(target_node_route, timeout=30)
|
|
|
|
#print(f"{response =}")
|
|
|
|
if response.status_code == 200:
|
|
|
|
print(f"{target_host} is online and ready.")
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
print(f"{target_host} is not responding correctly {response.status_code}.")
|
|
|
|
return False
|
|
|
|
except requests.exceptions.RequestException as re:
|
|
|
|
#print(f"Error: {str(re)}")
|
|
|
|
print(f"Target Host {target_host} is offline.")
|
|
|
|
if wake_if_offline:
|
|
|
|
send_wakeonlan(target_host)
|
|
|
|
print(f"Host {target_host} wird aufgeweckt. Warten auf Hochfaren...(1min)")
|
|
|
|
time.sleep(60)
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
|
|
def get_target_ips():
|
|
|
|
targets = {
|
|
|
|
'nuc_morroc': '192.168.178.53'
|
|
|
|
}
|
|
|
|
return targets
|
|
|
|
|
|
|
|
|
|
|
|
def get_target_macs():
|
|
|
|
targets = {
|
|
|
|
'nuc_morroc': '48:21:0B:3D:46:D5'
|
|
|
|
}
|
|
|
|
return targets
|
|
|
|
|
|
|
|
|
|
|
|
def send_wakeonlan(target_host: str):
|
|
|
|
mac_profiles = get_target_macs()
|
|
|
|
mac_target = mac_profiles[target_host]
|
|
|
|
send_magic_packet(mac_target)
|
|
|
|
print(f"Magic packet wurde an {target_host} gesendet")
|
|
|
|
|
|
|
|
|
|
|
|
def _send_command_to_target(target_ip: str, command: str = 'testcommand') -> str:
|
|
|
|
target_node_route = f'http://{target_ip}:9713/osiris_execute'
|
|
|
|
response = requests.post(target_node_route, json={'command': command})
|
|
|
|
result = response.json()
|
|
|
|
try:
|
|
|
|
if response.status_code == 200:
|
|
|
|
print('Response:', result.get('message'))
|
|
|
|
else:
|
|
|
|
print('Failed to execute script:', result.get('message'))
|
|
|
|
except requests.exceptions.RequestException as e:
|
|
|
|
print(f"Error connecting to the host {target_ip}: {e}")
|
|
|
|
|
|
|
|
|
|
|
|
def send_shutdown_command(target_host: str):
|
|
|
|
print("Shutdown-Command an Ziel-Hostes gesendet.")
|
|
|
|
ip_profiles = get_target_ips()
|
|
|
|
ip_target = ip_profiles[target_host]
|
|
|
|
_send_command_to_target(ip_target,'shutdown')
|
|
|
|
|
|
|
|
|
|
|
|
def send_testcommand(target_host: str):
|
|
|
|
print("Test-Command an Ziel-Hostes gesendet.")
|
|
|
|
ip_profiles = get_target_ips()
|
|
|
|
ip_target = ip_profiles[target_host]
|
|
|
|
_send_command_to_target(ip_target)
|
|
|
|
|
|
|
|
|
|
|
|
def process_arguments():
|
|
|
|
parser = argparse.ArgumentParser(description="Osiris Node Skript kommuniziert mit Osiris Listener bei Target Host.")
|
|
|
|
parser.add_argument('target_host', type=str, choices=["nuc_morroc"], help='Profil des Ziel-Hostes')
|
|
|
|
parser.add_argument('command', type=str, choices=['testcommand', 'shutdown', 'status'], help='Command to send to the target host')
|
|
|
|
parser.add_argument('-w', '--wake', action='store_true', help='Weckt Ziel-Host auf wenn offline')
|
|
|
|
|
|
|
|
args = parser.parse_args()
|
|
|
|
|
|
|
|
target_host = args.target_host
|
|
|
|
#target_host = 'nuc_morroc'
|
|
|
|
wake = args.wake
|
|
|
|
|
|
|
|
match args.command:
|
|
|
|
case "status":
|
|
|
|
assert_availibilty(target_host,wake)
|
|
|
|
case "shutdown":
|
|
|
|
assert_availibilty(target_host, wake_if_offline=True)
|
|
|
|
send_shutdown_command(target_host)
|
|
|
|
case "testcommand":
|
|
|
|
assert_availibilty(target_host, wake_if_offline=True)
|
|
|
|
send_testcommand(target_host)
|
|
|
|
case _:
|
|
|
|
print("Kein gültiger Command. Programm wird beendet.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
process_arguments()
|
|
|
|
|