Gorcupt

4-1

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