Skip to content

Instantly share code, notes, and snippets.

@johan-bjareholt
Last active September 15, 2015 18:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save johan-bjareholt/3488b2fe7626134aab48 to your computer and use it in GitHub Desktop.
Save johan-bjareholt/3488b2fe7626134aab48 to your computer and use it in GitHub Desktop.
#!/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()
@danestig
Copy link

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.

@johan-bjareholt
Copy link
Author

Should be fixed now, try again

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment