Skip to content

Instantly share code, notes, and snippets.

@bwindsor
Created December 7, 2021 18:15
Show Gist options
  • Save bwindsor/079ff2a2b780fb23c98a2490531a52b3 to your computer and use it in GitHub Desktop.
Save bwindsor/079ff2a2b780fb23c98a2490531a52b3 to your computer and use it in GitHub Desktop.
Ping many IP addresses in parallel to see which respond to a ping. Useful for finding IPs of another device you put on the network.
import threading
import subprocess
def f(x: int) -> None:
try:
output = subprocess.check_output(["ping", "-n", "1", f"192.168.1.{x}"])
if b"unreachable" not in output:
print(f"{x} was successful")
except subprocess.CalledProcessError:
pass
threads = [threading.Thread(target=f, args=(i,)) for i in range(1, 255)]
for t in threads:
t.start()
for t in threads:
t.join()
print("Done")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment