From c65f2ddfa534ccad3b05b987e9dfee536576c83f Mon Sep 17 00:00:00 2001 From: Alex <4lexkidd@iamsander.de> Date: Tue, 9 Jul 2024 23:41:13 +0200 Subject: [PATCH] fix check avalibility & add wol & shutdown --- osiris_node.py | 48 +++++++++++++++++++++++------------------------- 1 file changed, 23 insertions(+), 25 deletions(-) diff --git a/osiris_node.py b/osiris_node.py index 0c2f3fb..03caf89 100644 --- a/osiris_node.py +++ b/osiris_node.py @@ -8,22 +8,19 @@ def check_target_available(target_host: str): # Ping the host to check if it's online ips = get_target_ips() ip_target = ips[target_host] + target_node_route = f'http://{ip_target}:9713/status' try: - # Use subprocess to execute the ping command - response = subprocess.run( - ["ping", "-n", "1", "-w", "1000", ip_target], # Windows parameters: '-n 1' sends one ping, '-w 1000' sets a 1-second timeout - stdout=subprocess.PIPE, - stderr=subprocess.PIPE - ) - return response.returncode == 0 - except Exception as e: - print(f"Error pinging the host: {e}") + response = requests.get(target_node_route, timeout=30) + #print(f"{response =}") + return response.status_code == 200 + except requests.exceptions.RequestException as re: + #print(f"Error: {str(re)}") return False def get_target_ips(): targets = { - 'nuc_morroc': '192.168.178.52' + 'nuc_morroc': '192.168.178.53' } return targets @@ -45,12 +42,12 @@ def send_wakeonlan(target_host: str): 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: - result = response.json() print('Response:', result.get('message')) else: - print('Failed to execute script:', response.json()) + print('Failed to execute script:', result.get('message')) except requests.exceptions.RequestException as e: print(f"Error connecting to the host {target_host}: {e}") @@ -68,17 +65,18 @@ def send_testcommand(target_host: str): if __name__ == "__main__": - target_host = 'nuc_morroc' - available = check_target_available(target_host) - if available: - send_testcommand(target_host) - else: + target_host = 'nuc_morroc' + print("Verfügbarkeit des Ziel-Hostes wird überprüft.") + if not check_target_available(target_host): send_wakeonlan(target_host) - print(f"Host {target_host} wird auf geweckt") - time.sleep(30) - available = check_target_available(target_host) - if not available: - print(f"Host {target_host} reagiert nicht auf weckruf. Programm wird beendet.") - else: - print(f"Host {target_host} ist aufgewacht. Kommando wird gesendet.") - send_testcommand(target_host) \ No newline at end of file + print(f"Host {target_host} wird aufgeweckt. Warten auf Hochfaren...") + time.sleep(60) + + if check_target_available(target_host): + #send_testcommand(target_host) + #print(f"TestCommand an Host {target_host} gesendet.") + send_shutdown_command(target_host) + print(f"Shutdown an Host {target_host} gesendet.") + else: + print(f"Host {target_host} ist offline. Programm wird beendet.") + \ No newline at end of file