# -*- coding: utf-8 -*- r'''阶段4:诊断局域网直连为什么没生效。 现象(手表日志): 已更新电脑地址为 192.168.2.64:8765 电脑不在同一网络,继续用中转 判断逻辑在手表端 MainActivity: pickSameSubnet(client.relayLanIps()) <- 拿电脑报上来的 lanIps localIpv4List() <- 拿手表自己的网卡 IP sameSubnet(a, b) <- 比前 3 段(/24) 可能出问题的地方: 1. 被控端报的 lanIps 里全是虚拟网卡(VMware/VirtualBox/Hyper-V) —— 真实的 192.168.2.64 反而不在里面 2. 手表自己的 localIpv4List() 拿到的是运营商 IP(移动数据) 3. 手表和电脑真的不在一个网段 这个脚本查第 1 条:本机被控端实际会报哪些 IP。 第 2、3 条要手表端日志,我先把能查的查清。 ''' from __future__ import annotations import json import socket import subprocess import sys import time from pathlib import Path ROOT = Path(r"C:\Users\shu\Desktop\deepseekagent\LanRemote") sys.path.insert(0, str(ROOT / "windows")) def main() -> int: print(" 一、被控端会报哪些 lanIps(这才是关键)") try: from lanremote.discovery import local_ipv4_list ips = local_ipv4_list() print(" local_ipv4_list() -> %s" % (ips,)) except Exception as exc: # noqa: BLE001 print(" 拿不到(%s),换个方式" % exc) ips = [] # 直接列出所有网卡地址,对照着看 print() print(" 二、本机所有 IPv4 地址(含虚拟网卡)") try: result = subprocess.run( ["powershell", "-NoProfile", "-Command", "Get-NetIPAddress -AddressFamily IPv4 | " "Where-Object { $_.IPAddress -notlike '127.*' } | " "ForEach-Object { $_.IPAddress + ' ' + $_.InterfaceAlias }"], capture_output=True, text=True, timeout=60, encoding="utf-8", errors="replace") for line in (result.stdout or "").splitlines(): if line.strip(): print(" " + line.strip()) except Exception as exc: # noqa: BLE001 print(" 失败: %s" % exc) print() print(" 三、判断:报出去的 IP 里有没有真实局域网地址") real = [ip for ip in ips if ip.startswith(("192.168.", "10.", "172.16.", "172.17.", "172.18.", "172.19.", "172.2", "172.30.", "172.31.")) and not ip.startswith("169.254.")] virtual_hint = [ip for ip in ips if ip.startswith(("192.168.56.", "192.168.99.", "172.17.", "172.18.", "172.19."))] print(" 报出去的: %s" % (ips or "(空)")) print(" 像真实局域网的: %s" % (real or "(没有!)")) print(" 像虚拟网卡的: %s" % (virtual_hint or "(没有)")) if not ips: print() print(" ⚠ 一个都没报 —— 手表当然匹配不上。") elif not real: print() print(" ⚠ 报的全是虚拟网卡地址,手表连不到这些。") else: print() print(" OK 至少有一个真实局域网地址,手表应该能匹配到 %s" % real[0]) print() print(" 四、手表端要看的日志关键字") print(" 手表连上后,日志区会打:") print(" 「已更新电脑地址为 x.x.x.x:8765」—— 这是被控端报的地址") print(" 「电脑不在同一网络,继续用中转」—— 说明 sameSubnet 没匹配上") print(" 要确认两件事:") print(" 1. 那个 x.x.x.x 是不是电脑真实的局域网地址") print(" 2. 手表自己的 WiFi 地址是不是同一个 /24") return 0 if __name__ == "__main__": sys.exit(main())