Created
June 24, 2023 13:36
-
-
Save duracell80/b23f9f47dcca364df195f82a3d63b187 to your computer and use it in GitHub Desktop.
Python find active hosts on LAN
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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