"""Сопоставляет MAC телевизоров с IP из DHCP-аренд Keenetic.""" import hashlib import http.cookiejar import json import urllib.error import urllib.request BASE = "http://192.168.1.1" CREDS = json.load(open("/root/.keenetic.json", encoding="utf-8")) jar = http.cookiejar.CookieJar() op = urllib.request.build_opener(urllib.request.HTTPCookieProcessor(jar)) try: r = op.open(urllib.request.Request(BASE + "/auth"), timeout=10) ch, realm = r.headers.get("X-NDM-Challenge"), r.headers.get("X-NDM-Realm") except urllib.error.HTTPError as e: ch, realm = e.headers.get("X-NDM-Challenge"), e.headers.get("X-NDM-Realm") md5 = hashlib.md5(f"{CREDS['login']}:{realm}:{CREDS['password']}".encode()).hexdigest() sha = hashlib.sha256((ch + md5).encode()).hexdigest() op.open(urllib.request.Request(BASE + "/auth", data=json.dumps({"login": CREDS["login"], "password": sha}).encode(), method="POST", headers={"Content-Type": "application/json"}), timeout=10) def get(path): with op.open(urllib.request.Request(BASE + path), timeout=20) as r: return json.loads(r.read() or b"{}") dhcp = get("/rci/ip/dhcp") hosts = dhcp.get("host") or [] if isinstance(hosts, dict): hosts = list(hosts.values()) TARGETS = { "44:ea:30": "Samsung", "7c:49:eb": "Xiaomi", "e8:51:77": "Hisense/Qingdao", "48:9e:9d": "тв Родители кабинет", "c4:8b:66": "Chromecast Гостиная", "14:ea:63": "Chromecast Smart TV Pro", "d4:f7:d5": "PlayStation 5", "50:8b:b9": "Tuya", } print(f" аренд всего: {len(hosts)}") for h in hosts: mac = (h.get("mac") or "").lower() for pref, label in TARGETS.items(): if mac.startswith(pref): print(f" {str(h.get('ip') or h.get('address')):<16} {mac:<18} {label} " f"| имя: {(h.get('name') or '—')[:40]}")