|
|
|
@ -1,21 +1,36 @@
|
|
|
|
|
import requests
|
|
|
|
|
from wakeonlan import send_magic_packet
|
|
|
|
|
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
|
|
|
|
|
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'
|
|
|
|
|
available = False
|
|
|
|
|
try:
|
|
|
|
|
response = requests.get(target_node_route, timeout=30)
|
|
|
|
|
#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:
|
|
|
|
|
#print(f"Error: {str(re)}")
|
|
|
|
|
return False
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_target_ips():
|
|
|
|
@ -49,34 +64,47 @@ def _send_command_to_target(target_ip: str, command: str = 'testcommand') -> str
|
|
|
|
|
else:
|
|
|
|
|
print('Failed to execute script:', result.get('message'))
|
|
|
|
|
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):
|
|
|
|
|
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')
|
|
|
|
|
|
|
|
|
|
args = parser.parse_args()
|
|
|
|
|
|
|
|
|
|
target_host = args.target_host
|
|
|
|
|
#target_host = 'nuc_morroc'
|
|
|
|
|
|
|
|
|
|
match args.command:
|
|
|
|
|
case "status":
|
|
|
|
|
assert_availibilty(target_host)
|
|
|
|
|
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__":
|
|
|
|
|
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 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.")
|
|
|
|
|
|
|
|
|
|
process_arguments()
|
|
|
|
|
|