47 lines
1.1 KiB
Python
47 lines
1.1 KiB
Python
import ipaddress
|
|
from ping3 import ping
|
|
import socket
|
|
import time
|
|
import subprocess as sp
|
|
|
|
def get_ip():
|
|
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
|
s.settimeout(0)
|
|
try:
|
|
# doesn't even have to be reachable
|
|
s.connect(('10.254.254.254', 1))
|
|
IP = s.getsockname()[0]
|
|
except Exception:
|
|
IP = '127.0.0.1'
|
|
finally:
|
|
s.close()
|
|
return IP
|
|
|
|
if __name__ == "__main__":
|
|
ip = get_ip()
|
|
print("IP:", ip)
|
|
try:
|
|
ping(ip, timeout=1)
|
|
except:
|
|
print("could not ping self")
|
|
try:
|
|
r = sp.run(['ping', '-c', '4', ip])
|
|
print(r.returncode)
|
|
except:
|
|
print("ping command not available")
|
|
|
|
network = ipaddress.ip_network('192.168.1.0/24') # Creates subnet object
|
|
for ip in network:
|
|
try:
|
|
p = ping(str(ip), timeout=1)
|
|
if p is not None:
|
|
print("Found host at:", ip)
|
|
except:
|
|
r = sp.run(['ping', '-c', '4', ip])
|
|
if r.returncode == 0:
|
|
print("Found host at:", ip)
|
|
|
|
print("DONE")
|
|
while True:
|
|
time.sleep(10)
|