53 lines
1.3 KiB
Python
53 lines
1.3 KiB
Python
# SPDX-FileCopyrightText: 2022-present U.N. Owen <void@some.where>
|
|
#
|
|
# SPDX-License-Identifier: MIT
|
|
import ipaddress
|
|
import socket
|
|
from typing import List
|
|
from colorama import init, Fore
|
|
from icmplib import async_multiping, ping, multiping
|
|
|
|
init()
|
|
GREEN = Fore.GREEN
|
|
RESET = Fore.RESET
|
|
GRAY = Fore.LIGHTBLACK_EX
|
|
|
|
def are_alive(addresses: List[str]) -> List[str]:
|
|
hosts = multiping(addresses)
|
|
return [ host.address for host in hosts if host.is_alive ]
|
|
|
|
def is_host_alive(host: str) -> bool:
|
|
if ping(host, timeout=1, count=1).is_alive:
|
|
return True
|
|
else:
|
|
print(f"{GRAY}{host:15} is not alive {RESET}")
|
|
return False
|
|
|
|
|
|
def is_port_open(host: str, port: int) -> bool:
|
|
try:
|
|
s = socket.socket()
|
|
s.connect((host, int(port)))
|
|
except ConnectionRefusedError:
|
|
print(f"{GRAY}{host:15}:{port:5} is closed {RESET}")
|
|
except TimeoutError:
|
|
print(f"{GRAY}{host:15}:{port:5} is not alive {RESET}")
|
|
else:
|
|
print(f"{GREEN}{host:15}:{port:5} is open {RESET}")
|
|
finally:
|
|
s.close()
|
|
|
|
|
|
def is_subnet(ip: str) -> bool:
|
|
if "/" in ip:
|
|
return True
|
|
else:
|
|
return False
|
|
|
|
|
|
def hosts_in_subnet(network: str) -> List:
|
|
if not is_subnet:
|
|
print(f"{network} is not a network")
|
|
|
|
return list(ipaddress.ip_network(network).hosts())
|