"""Поиск камер в локальной сети. Смотрит соседей в ARP-таблице и проверяет типичные порты камер: 554 (RTSP), 8000/8080 (веб-интерфейс Hikvision/Dahua), 8554. Ничего не меняет — только читает. """ import json import os import socket import subprocess import sys import urllib.request from concurrent.futures import ThreadPoolExecutor CORE = os.environ.get("CORE_URL", "http://127.0.0.1:9120") PORTS = [554, 8554, 8000, 8080, 37777] # 37777 — Dahua OUI_VENDORS = { "b0c554": "Hikvision", "4cbd8f": "Hikvision", "c0562d": "Hikvision", "3cef8c": "Dahua", "e0508b": "Dahua", "9c144d": "Dahua", "001c27": "TP-Link", "a0f3c1": "TP-Link", "2c3ae8": "Xiaomi", "64cc2e": "Xiaomi", "78 11dc": "Xiaomi", "bcd177": "Intelbras", "e0b9ba": "Apple", } def neighbours(): """Соседи из ARP: ip, mac, интерфейс.""" out = subprocess.run(["ip", "neigh"], capture_output=True, text=True) hosts = [] for line in (out.stdout or "").splitlines(): parts = line.split() if len(parts) < 3: continue ip = parts[0] if not ip.startswith("192.168."): continue mac = "" for i, p in enumerate(parts): if p == "lladdr" and i + 1 < len(parts): mac = parts[i + 1].lower() state = parts[-1] if state in ("FAILED", "INCOMPLETE"): continue hosts.append({"ip": ip, "mac": mac, "state": state}) return hosts def vendor(mac): if not mac: return "" prefix = mac.replace(":", "")[:6] return OUI_VENDORS.get(prefix, "") def probe(host): """Открытые порты у хоста.""" open_ports = [] for port in PORTS: s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.settimeout(0.6) try: if s.connect_ex((host, port)) == 0: open_ports.append(port) finally: s.close() return host, open_ports def main(): hosts = neighbours() print(f"соседей в сети: {len(hosts)}") results = [] with ThreadPoolExecutor(max_workers=16) as ex: for host, ports in ex.map(probe, [h["ip"] for h in hosts]): if ports: mac = next((h["mac"] for h in hosts if h["ip"] == host), "") results.append({ "ip": host, "mac": mac, "vendor": vendor(mac), "ports": ports, "camera_like": 554 in ports or 8554 in ports or 37777 in ports, }) cameras = [r for r in results if r["camera_like"]] others = [r for r in results if not r["camera_like"]] print(f"\nпохоже на камеры: {len(cameras)}") for c in cameras: print(f" {c['ip']:15} {c['vendor'] or '?'} порты: {c['ports']}") print(f"\nс открытыми портами, но не камеры: {len(others)}") for c in others[:8]: print(f" {c['ip']:15} {c['vendor'] or '?'} порты: {c['ports']}") # сохраняем в ядро payload = {"key": "scan", "payload": {"hosts": len(hosts), "cameras": cameras, "others": others}, "tags": ["камеры", "сеть"]} try: req = urllib.request.Request(f"{CORE}/data/cameras", data=json.dumps(payload, ensure_ascii=False).encode(), method="POST", headers={"Content-Type": "application/json"}) urllib.request.urlopen(req, timeout=15).read() print("\nсохранено в ядро: коллекция cameras") except Exception as e: print("не сохранил:", str(e)[:80]) return 0 if __name__ == "__main__": sys.exit(main())