Advertisement
noctual

Untitled

Mar 27th, 2022
261
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.15 KB | None | 0 0
  1. import ping3
  2. import socket
  3. import graphviz
  4. from ipaddress import IPv4Network
  5. from concurrent.futures import ThreadPoolExecutor, as_completed
  6.  
  7. def ping(ip):
  8.     try:
  9.         available = ping3.ping(ip)
  10.     except Exception:
  11.         available = False
  12.     return (ip, available)
  13.  
  14. def getAvailableIP(network):
  15.     result = []
  16.     with ThreadPoolExecutor() as executor:
  17.         futures = []
  18.         for ip in IPv4Network(network, strict=True):
  19.             futures.append(executor.submit(ping, str(ip)))
  20.         for future in as_completed(futures):
  21.             ip, available = future.result()
  22.             if available:
  23.                 result.append((ip, available))
  24.     return result
  25.  
  26. if __name__ == '__main__':
  27.     my_ip = socket.gethostbyname(socket.gethostname())
  28.     all_ip = getAvailableIP('192.168.1.0/255.255.255.0')
  29.     dot = graphviz.Digraph('round-table', comment='The Round Table')
  30.     dot.node('my_ip', label=my_ip)
  31.     for i, ip in enumerate(all_ip):
  32.         if ip[0] != my_ip:
  33.             dot.node(f'ip{i}', label=ip[0])
  34.             dot.edge('my_ip', f'ip{i}', label=str(round(ip[1],6)))
  35.     dot.render(directory='doctest-output', view=True)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement