From 1aad27b937bb576dff652d1969685cbde7f7e6fa Mon Sep 17 00:00:00 2001 From: vgallegoiz Date: Mon, 8 Apr 2024 23:32:57 +0200 Subject: [PATCH] Created toIP function and modified check_ping to work with ipaddress --- status.py | 36 ++++++++++++++++++++++++++---------- 1 file changed, 26 insertions(+), 10 deletions(-) diff --git a/status.py b/status.py index 0ff9d96..151f10e 100644 --- a/status.py +++ b/status.py @@ -18,9 +18,7 @@ def parseArguments(): try: # Parsear los argumentos de la línea de comandos args = parser.parse_args() - if args.web and args.host: - return args.host, args.web - elif args.host: + if args.host: return args.host elif args.web: return args.web @@ -31,14 +29,32 @@ def parseArguments(): parser.print_help() exit() -def check_ping(host): - try: - response = ping(host, count=4, timeout=2) # verbose=True -> Para ver la respuesta del ping - return True if response.success() else False - except Exception as e: - print(f"Error making ping to {host}: {e}") - return False +def toIP(hosts): + ips = set() + for host in hosts: + try: + ip = ipaddress.IPv4Address(host) + ips.add(ip) + except ipaddress.AddressValueError: + print(f"La entrada '{ip}' no es una dirección IP válida.") + return ips + +def check_ping(hosts): + for host in hosts: + try: + print(f'Pingin host: {host}') + response = ping(str(host), count=4, timeout=2) # verbose=True -> Para ver la respuesta del ping + if not response.success(): + print(False) + # Ennviar un mensaje + else: + print(True) + except Exception as e: + print(f"Error making ping to {host}: {e}") if __name__ == '__main__': args = parseArguments() + hosts = read_document(args) + ips = toIP(hosts) + check_ping(ips) \ No newline at end of file