def get_hosts( net: ip.IPv4Network, port: int, service: str, timeout: float, debug: bool = False ) -> typing.Iterable[typing.Tuple[ip.IPv4Address, str]]: """ loops over hosts in network One thread per address. IPv6 is not well supported, it will overwhelm RAM except by a plain for loop. A different approach is needed to handle IPv6 scale, but it's fine for IPv4. """ if debug or isinstance(net, ip.IPv6Network): for host in net.hosts(): logging.debug(host) res = isportopen(host, port, service, timeout) if res: yield res else: with concurrent.futures.ThreadPoolExecutor(max_workers=100) as exc: futures = (exc.submit(isportopen, host, port, service, timeout) for host in net.hosts()) for future in concurrent.futures.as_completed(futures): res = future.result() if res: yield res
def get_hosts_seq(net: ip.IPv4Network, port: int, service: str, timeout: float) -> T.Iterable[tuple[ip.IPv4Address, str]]: """ find hosts sequentially (no parallelism or concurrency) """ for host in net.hosts(): res = isportopen(host, port, service, timeout) if res: yield res
async def get_hosts( net: ip.IPv4Network, port: int, service: str, timeout: float) -> typing.List[typing.Tuple[ip.IPv4Address, str]]: # print(list(net.hosts())) # all the addresses to be pinged # hosts = await as_completed(net, port, service, timeout) futures = [waiter(host, port, service, timeout) for host in net.hosts()] host_results = await asyncio.gather(*futures) hosts = list(filter(None, host_results)) return hosts
async def get_hosts( net: ip.IPv4Network, port: int, service: str, timeout: float ) -> list[tuple[ip.IPv4Address, str]]: hosts = [] for h in asyncio.as_completed([waiter(host, port, service, timeout) for host in net.hosts()]): host = await h if host: print(host) hosts.append(host) return hosts
def get_hosts(net: ip.IPv4Network, port: int, service: str, timeout: float) -> T.Iterable[tuple[ip.IPv4Address, str]]: """ loops over hosts in network One thread per address. """ with concurrent.futures.ThreadPoolExecutor(max_workers=100) as exc: futures = (exc.submit(isportopen, host, port, service, timeout) for host in net.hosts()) for future in concurrent.futures.as_completed(futures): res = future.result() if res: yield res
async def as_completed( net: ip.IPv4Network, port: int, service: str, timeout: float) -> typing.List[typing.Tuple[ip.IPv4Address, str]]: futures = [isportopen(host, port, service) for host in net.hosts()] hosts = [] for future in asyncio.as_completed(futures, timeout=timeout): try: res = await future except asyncio.TimeoutError: continue if res is not None: print(*res) hosts.append(res) return hosts
def scanhosts(net: ip.IPv4Network, port: int, service: str, timeout: float, debug: bool) -> List[ip.IPv4Address]: """ loops over xxx.xxx.xxx.1-254 IPv4 only. """ assert isinstance(net, ip.IPv4Network) print('searching', net) hosts = list(net.hosts()) if debug: servers = [h for h in hosts if isportopen(h, port, service, timeout)] else: with concurrent.futures.ThreadPoolExecutor(max_workers=100) as exc: portsopen = exc.map(isportopen, hosts, itertools.repeat(port), itertools.repeat(service)) servers = list(itertools.compress(hosts, portsopen)) return servers