Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save duracell80/b23f9f47dcca364df195f82a3d63b187 to your computer and use it in GitHub Desktop.
Save duracell80/b23f9f47dcca364df195f82a3d63b187 to your computer and use it in GitHub Desktop.
Python find active hosts on LAN
#!/usr/bin/python3
import os, socket
# Perform LAN scan
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect(("1.1.1.1", 80))
h = s.getsockname()[0].split(".")
h = str(h[0] + "." + h[1] + "." + h[2])
s.close()
hosts = []
print("[i] LAN scan in progress ... takes about 5 minutes")
for i in range(1,255):
host = h + "." + str(i)
result = str(os.popen("ping " + str(host) + " -w 1").read())
if "1 received" in result:
hosts.append(host)
print(hosts)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment