This is a quick and dirty utility to find up addresses on a 192.168.x.0 subnet like a home LAN.
It will try to guess the subnet if you don't specify an argument. Else the first argument is taken
as the x in 192.168.x.
#!/usr/bin/env python3
import socket
import sys
from subprocess import run, PIPE
from threading import Thread
from time import sleep
import platform
up = set()
hostname = socket.gethostname()
s = platform.system()
if "CYGWIN" in s or "Windows" in s:
windows = True
else:
windows = False
if windows:
def pingcmd(addr):
return ["ping","-n","1","-w","1",addr]
else:
def pingcmd(addr):
return ["ping","-c","1","-w","1",addr]
args = sys.argv[1:]
if len(args) > 0:
s1 = int(args[0])
else:
a = socket.getaddrinfo(hostname,80,family=socket.AF_INET)
subnets = set()
for x in a:
y = x[4]
z = y[0]
if "192.168" in z:
r,s,t,u = z.split(".")
subnets.add(int(t))
ss = list(sorted(subnets))
s1 = ss[0]
def tryping(n):
addr = f"192.168.{s1}.{n}"
m = run(pingcmd(addr),stdout=PIPE,stderr=PIPE)
if m.returncode == 0:
up.add(addr)
for i in range(2,254):
thread = Thread(target=lambda: tryping(i))
thread.start()
for i in range(3):
print(".",end="",file=sys.stderr)
sys.stderr.flush()
sleep(1)
print("",file=sys.stderr)
print("\n".join(sorted(up)))