Last active
September 15, 2015 18:30
-
-
Save johan-bjareholt/3488b2fe7626134aab48 to your computer and use it in GitHub Desktop.
This file contains 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/env python3 | |
import socket | |
import subprocess | |
import os | |
import netifaces | |
debug = True | |
def start(): | |
# Get gateway ip | |
gws = netifaces.gateways()['default'] | |
gwid = next (iter (gws)) | |
gatewayip = gws[gwid][0] | |
if debug == True: | |
print("Gateway ip: " + gatewayip) | |
# Prepare basemask | |
depth = 3 # Doesn't look for devices below 255.255.255.0 | |
baseip = gatewayip.split('.') | |
basemask = "" | |
for i in range(3): | |
basemask += baseip[i] + "." | |
# Find all devices | |
connected_ips = find_devices(basemask) | |
print("Connected devices: " + str(connected_ips)) | |
def find_devices(basemask): | |
connected_ips = [] | |
for i in range(255): | |
ip = basemask + str(i) | |
try: | |
socket.gethostbyaddr(ip) | |
connected_ips.append(ip) | |
if debug == True: | |
print("Found device: " + ip) | |
except socket.herror: | |
if debug == True: | |
print("Unknown host: " + ip) | |
return connected_ips | |
if __name__ == '__main__': | |
start() |
Should be fixed now, try again
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
My fork now returns the correct netmask on windows. But the recursion, test_rec(mask, depth), doesn't return in a reasonable amount of time.