fix check avalibility & add wol & shutdown

main
Alex 6 months ago
parent ef60d7a3c4
commit c65f2ddfa5

@ -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}")
@ -69,16 +66,17 @@ 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:
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)
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.")
Loading…
Cancel
Save