#!/bin/bash # 把中转上所有设备清空,让用户的电脑干干净净地认领 789113。 # # 背景:早期测试时被控端反复注册,留下了 7 台陈旧设备, # 而 789113 被其中某个绑定占着 —— 用户真正的电脑反而用不了自己的码。 set -u BASE=http://127.0.0.1:8080 KEY=$(cat /root/lanremote-relay/data/admin_key.txt 2>/dev/null) echo "===== 1. 清空之前 =====" curl -s --max-time 8 "$BASE/v1/health" > /tmp/before.json 2>/dev/null python3 - <<'PY' import json try: d = json.load(open('/tmp/before.json')) except Exception: raise SystemExit print(f" 设备 {len(d.get('devices', []))} 台,会话 {d.get('sessions')}") for x in d.get("devices", []): print(f" {x.get('code')} {x.get('name')}") PY echo echo "===== 2. 全部抹掉 =====" python3 - <<'PY' import json, subprocess try: d = json.load(open('/tmp/before.json')) except Exception: raise SystemExit key = open('/root/lanremote-relay/data/admin_key.txt').read().strip() n = 0 for x in d.get("devices", []): code = x.get("code") out = subprocess.run( ["curl", "-s", "--max-time", "8", "-X", "POST", "http://127.0.0.1:8080/v1/admin/forget", "-H", f"X-Admin: {key}", "-H", "Content-Type: application/json", "-d", json.dumps({"code": code})], capture_output=True, text=True).stdout ok = '"forgotten": true' in out or '"forgotten":true' in out print(f" 抹掉 {code} -> {'✓' if ok else out.strip()[:50]}") if ok: n += 1 print(f" 共抹掉 {n} 台") PY echo echo "===== 3. 重启中转,确保内存里的绑定也清干净 =====" systemctl restart lanremote-relay sleep 4 echo " 服务: $(systemctl is-active lanremote-relay)" echo echo "===== 4. 清空之后 =====" curl -s --max-time 8 "$BASE/v1/health" > /tmp/after.json 2>/dev/null python3 - <<'PY' import json try: d = json.load(open('/tmp/after.json')) except Exception: raise SystemExit print(f" 设备 {len(d.get('devices', []))} 台,会话 {d.get('sessions')}") PY echo echo "===== 5. 789113 现在能不能用 =====" R=$(curl -s --max-time 8 -X POST "$BASE/v1/agent/register" \ -H 'Content-Type: application/json' \ -d '{"deviceId":"probe-final","code":"789113","name":"探测","version":"1.4.0"}') echo " $R" | head -c 160 echo if echo "$R" | grep -q '"ok": *true'; then echo " ✓ 789113 空闲可用" curl -s -o /dev/null -X POST "$BASE/v1/admin/forget" \ -H "X-Admin: $KEY" -H 'Content-Type: application/json' \ -d '{"code":"789113"}' echo " (探测记录已清掉)" else echo " ✗ 还是被占" fi echo echo "===== 6. 最终状态 =====" curl -s --max-time 8 "$BASE/v1/health" > /tmp/final.json 2>/dev/null python3 - <<'PY' import json try: d = json.load(open('/tmp/final.json')) except Exception: raise SystemExit print(f" 设备 {len(d.get('devices', []))} 台 会话 {d.get('sessions')} " f"内存 {d.get('memoryKB', 0) // 1024} MB") for x in d.get("devices", []): print(f" {x.get('code')} {x.get('name')}") PY echo for s in lanremote-relay lanremote-tunnel lanremote-watchdog.timer; do printf " %-30s %-9s %s\n" "$s" \ "$(systemctl is-active $s 2>/dev/null)" \ "$(systemctl is-enabled $s 2>/dev/null)" done echo echo " 公网地址: $(cat /root/lanremote-relay/public_url.txt 2>/dev/null)" echo " 可达性: HTTP $(timeout 20 curl -4 -s -o /dev/null -w '%{http_code}' -H 'User-Agent: LanRemote/1.4.0' https://relay.xxmxxs.top/v1/health 2>/dev/null)"