|
|
|
import requests
|
|
|
|
from wakeonlan import send_magic_packet
|
|
|
|
import time
|
|
|
|
import subprocess
|
|
|
|
|
|
|
|
|
|
|
|
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:
|
|
|
|
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.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_host}: {e}")
|
|
|
|
|
|
|
|
|
|
|
|
def send_shutdown_command(target_host: str):
|
|
|
|
ip_profiles = get_target_ips()
|
|
|
|
ip_target = ip_profiles[target_host]
|
|
|
|
_send_command_to_target(ip_target,'shutdown')
|
|
|
|
|
|
|
|
|
|
|
|
def send_testcommand(target_host: str):
|
|
|
|
ip_profiles = get_target_ips()
|
|
|
|
ip_target = ip_profiles[target_host]
|
|
|
|
_send_command_to_target(ip_target)
|
|
|
|
|
|
|
|
|
|
|
|
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.")
|
|
|
|
|