Added feature to save the state of the host

This commit is contained in:
victor
2024-05-05 00:26:48 +02:00
parent 76069b8193
commit ff9edd855e

View File

@@ -40,17 +40,31 @@ def toIP(hosts):
print(f"La entrada '{host}' no es una dirección IP válida.") print(f"La entrada '{host}' no es una dirección IP válida.")
return ips return ips
def check_ping(hosts): def check_ping(hosts, fails):
for host in hosts: for host in hosts:
try: try:
response = ping(str(host), count=4, timeout=2) # verbose=True -> Para ver la respuesta del ping response = ping(str(host), count=4, timeout=2) # verbose=True -> Para ver la respuesta del ping
if not response.success(): if not response.success():
logging.info(f"Bad ping: {host}") logging.info(f"Bad ping: {host}")
if (host not in fails):
send_discord_message(f"Ping fallido para el host: {host}") send_discord_message(f"Ping fallido para el host: {host}")
# Añadir el host a fails
fails.add(host)
elif response.success(): elif response.success():
if host in fails:
fails.remove(host)
logging.info(f"Ping correct: {host}") logging.info(f"Ping correct: {host}")
except Exception as e: except Exception as e:
logging.info(f"Bad ping: {host}") 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): def send_discord_message(message):
data = { data = {
@@ -68,10 +82,12 @@ def send_discord_message(message):
if __name__ == '__main__': if __name__ == '__main__':
args = parseArguments() args = parseArguments()
ips_and_hosts = set() ips_and_hosts = set()
fails = set()
if args.host: if args.host:
ips_and_hosts.update(toIP(read_document(args.host))) ips_and_hosts.update(toIP(read_document(args.host)))
fails.update(toIP(read_document("failed.txt")))
if args.web: if args.web:
print("Hay webs") print("Hay webs")
# Parsear correctamente # Parsear correctamente
# ips_and_hosts.update() # ips_and_hosts.update()
check_ping(ips_and_hosts) check_ping(ips_and_hosts, fails)