add Argparse + Refactor

main
Alex 6 months ago
parent c65f2ddfa5
commit f97e43cdd9

@ -1,20 +1,35 @@
import requests import requests
from wakeonlan import send_magic_packet from wakeonlan import send_magic_packet
import time import time
import subprocess import argparse
def check_target_available(target_host: str): def assert_availibilty(target_host: str, wake_if_offline: bool = False):
# Ping the host to check if it's online # Ping the host to check if it's online
print("Verfügbarkeit des Ziel-Hostes wird überprüft.")
ips = get_target_ips() ips = get_target_ips()
ip_target = ips[target_host] ip_target = ips[target_host]
target_node_route = f'http://{ip_target}:9713/status' target_node_route = f'http://{ip_target}:9713/status'
available = False
try: try:
response = requests.get(target_node_route, timeout=30) response = requests.get(target_node_route, timeout=30)
#print(f"{response =}") #print(f"{response =}")
return response.status_code == 200 if response.status_code == 200:
print(f"{target_host} is online and ready.")
return True
else:
print(f"{target_host} is offline.")
return False
except requests.exceptions.RequestException as re: except requests.exceptions.RequestException as re:
#print(f"Error: {str(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:
print(f"{target_host} is offline.")
return False return False
@ -49,34 +64,47 @@ def _send_command_to_target(target_ip: str, command: str = 'testcommand') -> str
else: else:
print('Failed to execute script:', result.get('message')) print('Failed to execute script:', result.get('message'))
except requests.exceptions.RequestException as e: except requests.exceptions.RequestException as e:
print(f"Error connecting to the host {target_host}: {e}") print(f"Error connecting to the host {target_ip}: {e}")
def send_shutdown_command(target_host: str): def send_shutdown_command(target_host: str):
print("Shutdown-Command an Ziel-Hostes gesendet.")
ip_profiles = get_target_ips() ip_profiles = get_target_ips()
ip_target = ip_profiles[target_host] ip_target = ip_profiles[target_host]
_send_command_to_target(ip_target,'shutdown') _send_command_to_target(ip_target,'shutdown')
def send_testcommand(target_host: str): def send_testcommand(target_host: str):
print("Test-Command an Ziel-Hostes gesendet.")
ip_profiles = get_target_ips() ip_profiles = get_target_ips()
ip_target = ip_profiles[target_host] ip_target = ip_profiles[target_host]
_send_command_to_target(ip_target) _send_command_to_target(ip_target)
if __name__ == "__main__": def process_arguments():
target_host = 'nuc_morroc' parser = argparse.ArgumentParser(description="Osiris Node Skript kommuniziert mit Osiris Listener bei Target Host.")
print("Verfügbarkeit des Ziel-Hostes wird überprüft.") parser.add_argument('target_host', type=str, choices=["nuc_morroc"], help='Profil des Ziel-Hostes')
if not check_target_available(target_host): parser.add_argument('command', type=str, choices=['testcommand', 'shutdown', 'status'], help='Command to send to the target host')
send_wakeonlan(target_host)
print(f"Host {target_host} wird aufgeweckt. Warten auf Hochfaren...") args = parser.parse_args()
time.sleep(60)
if check_target_available(target_host): target_host = args.target_host
#send_testcommand(target_host) #target_host = 'nuc_morroc'
#print(f"TestCommand an Host {target_host} gesendet.")
match args.command:
case "status":
assert_availibilty(target_host)
case "shutdown":
assert_availibilty(target_host, wake_if_offline=True)
send_shutdown_command(target_host) send_shutdown_command(target_host)
print(f"Shutdown an Host {target_host} gesendet.") case "testcommand":
else: assert_availibilty(target_host, wake_if_offline=True)
print(f"Host {target_host} ist offline. Programm wird beendet.") send_testcommand(target_host)
case _:
print("Kein gültiger Command. Programm wird beendet.")
if __name__ == "__main__":
process_arguments()
Loading…
Cancel
Save