ethercat-linux/scripts/find_j1900.py

47 lines
1.1 KiB
Python
Raw Normal View History

#!/usr/bin/env python3
"""Find J1900 by MAC address on local subnet."""
import subprocess, sys
TARGET_MAC = "00:e2:69:34:71:2c"
SUBNET = "192.168.68"
def scan_arp_cache():
out = subprocess.check_output(["ip", "neigh", "show"], text=True)
for line in out.strip().split("\n"):
if TARGET_MAC.lower() in line.lower():
ip = line.split()[0]
return ip
return None
def ping_sweep():
procs = []
for i in range(1, 255):
ip = f"{SUBNET}.{i}"
p = subprocess.Popen(
["ping", "-c", "1", "-W", "0.2", ip],
stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL
)
procs.append(p)
for p in procs:
p.wait()
def main():
ip = scan_arp_cache()
if ip:
print(f"{ip}")
return 0
print(f"MAC not in ARP cache, scanning {SUBNET}.0/24 ...", file=sys.stderr)
ping_sweep()
ip = scan_arp_cache()
if ip:
print(f"{ip}")
return 0
print(f"NOT FOUND: {TARGET_MAC}", file=sys.stderr)
return 1
if __name__ == "__main__":
sys.exit(main())