Compare commits

4 Commits

Author SHA1 Message Date
4693cf74b2 use variable name instead of underscore in list comprehension
By convention, in Python we use underscores for "throwaway" variables, for example when unpacking a tuple only a few of whose members we're interested in.
2022-11-02 15:14:51 +01:00
5ed8482dcb Merge pull request #2 from zevaverbach/patch-2
bugfix and shortened a few functions
2022-11-02 15:11:53 +01:00
0a96dff67f Merge pull request #1 from zevaverbach/patch-1
compare boolean result using `is` operator instead of `==`
2022-11-02 15:11:27 +01:00
8b5a88409e bugfix and shortened a few functions
The bug was on line 48, which had `if not is_subnet`, checking the truthiness of the `is_subnet` function rather than calling it with `network`.

The other changes shorten up function bodies by, variously, removing an intermediate variable, returning early in a function returning a boolean, and simplifying `is_subnet` to a single line.
2022-11-02 15:09:31 +01:00

View File

@@ -4,25 +4,26 @@
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]
return [host.address for host in multiping(addresses) if host.is_alive]
def is_host_alive(host: str) -> bool:
if ping(host, timeout=1, count=1).is_alive:
return True
else:
if not ping(host, timeout=1, count=1).is_alive:
print(f"{GRAY}{host:15} is not alive {RESET}")
return False
return True
def is_port_open(host: str, port: int) -> bool:
@@ -40,14 +41,11 @@ def is_port_open(host: str, port: int) -> bool:
def is_subnet(ip: str) -> bool:
if "/" in ip:
return True
else:
return False
return "/" in ip
def hosts_in_subnet(network: str) -> List[str]:
if not is_subnet:
if not is_subnet(network):
print(f"{network} is not a network")
return [str(_) for _ in ipaddress.ip_network(network).hosts()]
return [str(host) for host in ipaddress.ip_network(network).hosts()]