#!/bin/bash # 清理中转上的测试设备,确认用户的配对码没被占。 set -u KEY=$(cat /root/lanremote-relay/data/admin_key.txt 2>/dev/null) echo "===== 1. 当前中转上有哪些设备 =====" curl -s --max-time 8 http://127.0.0.1:8080/v1/health > /tmp/h.json 2>/dev/null python3 - <<'PY' import json try: d = json.load(open('/tmp/h.json')) except Exception: print(" 读不到健康信息") raise SystemExit devices = d.get("devices", []) if not devices: print(" (一台都没有)") for x in devices: print(f" {x.get('code')} {x.get('name')} " f"online={x.get('online')} deviceId={x.get('deviceId')}") print(f" 共 {len(devices)} 台") PY echo echo "===== 2. 抹掉测试设备(名字里带 test/测试 的)=====" python3 - <<'PY' import json, subprocess try: d = json.load(open('/tmp/h.json')) except Exception: raise SystemExit key = open('/root/lanremote-relay/data/admin_key.txt').read().strip() removed = 0 for x in d.get("devices", []): name = str(x.get("name") or "") dev = str(x.get("deviceId") or "") if ("test" in dev.lower() or "测试" in name or "多地址" in name): 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 print(f" 抹掉 {code} ({name}) -> {out.strip()[:60]}") removed += 1 print(f" 共抹掉 {removed} 台") PY echo echo "===== 3. 现在还有哪些 =====" curl -s --max-time 8 http://127.0.0.1:8080/v1/health > /tmp/h2.json 2>/dev/null python3 - <<'PY' import json try: d = json.load(open('/tmp/h2.json')) except Exception: raise SystemExit for x in d.get("devices", []): print(f" {x.get('code')} {x.get('name')}") print(f" 共 {len(d.get('devices', []))} 台") PY echo echo "===== 4. 789113 能不能用 =====" RESULT=$(curl -s --max-time 8 -X POST http://127.0.0.1:8080/v1/agent/register \ -H 'Content-Type: application/json' \ -d '{"deviceId":"probe-check-789113","code":"789113","name":"探测","version":"1.4.0"}') echo " $RESULT" | head -c 200 echo if echo "$RESULT" | grep -q '"ok": *true'; then echo " ✓ 789113 可以用" # 探测完立刻抹掉,别占着 curl -s -o /dev/null -X POST http://127.0.0.1:8080/v1/admin/forget \ -H "X-Admin: $KEY" -H 'Content-Type: application/json' \ -d '{"code":"789113"}' echo " (探测记录已清掉,789113 现在是空闲的)" elif echo "$RESULT" | grep -q 'codeTaken'; then echo " ✗ 789113 被别的设备占了 —— 需要处理" else echo " ? 结果看不懂" fi echo echo "===== 5. 服务状态 =====" 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)" echo " 内存: $(free -m | awk 'NR==2{printf "已用 %s MB / 可用 %s MB", $3, $7}')"