import argparse from pythonping import ping import ipaddress def read_document(document_name): with open(document_name, 'r') as file: # Leer todas las líneas del archivo y guardarlas en un conjunto return {linea.rstrip('\n') for linea in file} def parseArguments(): # Crear un objeto ArgumentParser parser = argparse.ArgumentParser(description='Ejemplo de script con flags') # Agregar los argumentos con sus respectivos flags parser.add_argument('-W', '--web', action='store', dest='web', help='Indicar un archivo con lista de Webs') parser.add_argument('-H', '--host', action='store', dest='host', help='Indicar un archivo con lista de hosts') try: # Parsear los argumentos de la línea de comandos args = parser.parse_args() if args.host: return args.host elif args.web: return args.web except argparse.ArgumentError as e: # Si ocurre un error al analizar los argumentos, mostrar un mensaje de error print("Error:", e) # Mostrar el mensaje de ayuda parser.print_help() exit() 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)