|
|
|
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]
|
|
|
|
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}")
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
|
|
def get_target_ips():
|
|
|
|
targets = {
|
|
|
|
'nuc_morroc': '192.168.178.52'
|
|
|
|
}
|
|
|
|
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})
|
|
|
|
try:
|
|
|
|
if response.status_code == 200:
|
|
|
|
result = response.json()
|
|
|
|
print('Response:', result.get('message'))
|
|
|
|
else:
|
|
|
|
print('Failed to execute script:', response.json())
|
|
|
|
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'
|
|
|
|
available = check_target_available(target_host)
|
|
|
|
if available:
|
|
|
|
send_testcommand(target_host)
|
|
|
|
else:
|
|
|
|
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)
|