From ff9edd855e2267912894940de68cd2acf40eef51 Mon Sep 17 00:00:00 2001 From: victor Date: Sun, 5 May 2024 00:26:48 +0200 Subject: [PATCH] Added feature to save the state of the host --- status.py | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/status.py b/status.py index f3b3f97..e60dc0f 100644 --- a/status.py +++ b/status.py @@ -40,17 +40,31 @@ def toIP(hosts): print(f"La entrada '{host}' no es una dirección IP válida.") return ips -def check_ping(hosts): +def check_ping(hosts, fails): for host in hosts: try: response = ping(str(host), count=4, timeout=2) # verbose=True -> Para ver la respuesta del ping if not response.success(): logging.info(f"Bad ping: {host}") - send_discord_message(f"Ping fallido para el host: {host}") + if (host not in fails): + send_discord_message(f"Ping fallido para el host: {host}") + # Añadir el host a fails + fails.add(host) elif response.success(): + if host in fails: + fails.remove(host) logging.info(f"Ping correct: {host}") except Exception as e: logging.info(f"Bad ping: {host}") + print(e) + print(fails) + save_status(fails) + +def save_status(fails): + with open("failed.txt", "w") as file: + for fail in fails: + file.write(str(fail) + "\n") + file.close def send_discord_message(message): data = { @@ -68,10 +82,12 @@ def send_discord_message(message): if __name__ == '__main__': args = parseArguments() ips_and_hosts = set() + fails = set() if args.host: ips_and_hosts.update(toIP(read_document(args.host))) + fails.update(toIP(read_document("failed.txt"))) if args.web: print("Hay webs") # Parsear correctamente # ips_and_hosts.update() - check_ping(ips_and_hosts) \ No newline at end of file + check_ping(ips_and_hosts, fails) \ No newline at end of file