Advertisement
noctual

Untitled

Mar 27th, 2022
276
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.72 KB | None | 0 0
  1. import ping3
  2. from ipaddress import IPv4Network
  3. from concurrent.futures import ThreadPoolExecutor, as_completed
  4.  
  5. def ping(ip):
  6.     try:
  7.         available = ping3.ping(ip)
  8.     except Exception:
  9.         available = False
  10.     return (ip, available)
  11.  
  12. def getAvailableIP(network):
  13.     result = []
  14.     with ThreadPoolExecutor() as executor:
  15.         futures = []
  16.         for ip in IPv4Network(network, strict=True):
  17.             futures.append(executor.submit(ping, str(ip)))
  18.         for future in as_completed(futures):
  19.             ip, available = future.result()
  20.             if available:
  21.                 result.append(ip)
  22.     return result
  23.  
  24. if __name__ == '__main__':
  25.     print(getAvailableIP('192.168.1.0/255.255.255.0'))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement