Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import ping3
- import socket
- import graphviz
- from ipaddress import IPv4Network
- from concurrent.futures import ThreadPoolExecutor, as_completed
- def ping(ip):
- try:
- available = ping3.ping(ip)
- except Exception:
- available = False
- return (ip, available)
- def getAvailableIP(network):
- result = []
- with ThreadPoolExecutor() as executor:
- futures = []
- for ip in IPv4Network(network, strict=True):
- futures.append(executor.submit(ping, str(ip)))
- for future in as_completed(futures):
- ip, available = future.result()
- if available:
- result.append((ip, available))
- return result
- if __name__ == '__main__':
- my_ip = socket.gethostbyname(socket.gethostname())
- all_ip = getAvailableIP('192.168.1.0/255.255.255.0')
- dot = graphviz.Digraph('round-table', comment='The Round Table')
- dot.node('my_ip', label=my_ip)
- for i, ip in enumerate(all_ip):
- if ip[0] != my_ip:
- dot.node(f'ip{i}', label=ip[0])
- dot.edge('my_ip', f'ip{i}', label=str(round(ip[1],6)))
- dot.render(directory='doctest-output', view=True)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement