You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

82 lines
2.5 KiB
Python

import requests
6 months ago
from wakeonlan import send_magic_packet
import time
import subprocess
6 months ago
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'
6 months ago
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)}")
6 months ago
return False
6 months ago
def get_target_ips():
targets = {
'nuc_morroc': '192.168.178.53'
6 months ago
}
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()
6 months ago
try:
if response.status_code == 200:
print('Response:', result.get('message'))
else:
print('Failed to execute script:', result.get('message'))
6 months ago
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):
6 months ago
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.")