"""Read local Windows IPv4 interfaces without sending device discovery packets.""" from __future__ import annotations import importlib.util import json from pathlib import Path import sys def discovery_helper(): path = Path(__file__).resolve().parent / "skill" / "scripts" / "vendor" / "v2_discover.py" name = "artemis_device_test_network_discovery" spec = importlib.util.spec_from_file_location(name, path) module = importlib.util.module_from_spec(spec) sys.modules[name] = module spec.loader.exec_module(module) return module def inventory() -> dict: helper = discovery_helper() interfaces = [] eligible = [] for interface in helper.powershell_interfaces(): network = interface.network reason = helper.exclusion_reason(interface, include_virtual=False) if not reason and ( network.prefixlen > 30 or interface.address in {network.network_address, network.broadcast_address} ): reason = "没有可用的 IPv4 广播主机地址" cidr = f"{interface.address}/{interface.prefix_length}" interfaces.append( { "alias": interface.alias, "description": interface.description, "ipv4_interface": cidr, "network": str(network), "broadcast": str(network.broadcast_address), "hardware": interface.hardware, "eligible": reason is None, "exclusion_reason": reason, } ) if reason is None and cidr not in eligible: eligible.append(cidr) return { "status": "INTERFACES_ENUMERATED", "interfaces": interfaces, "eligible_interfaces": eligible, "device_discovery_executed": False, } def main() -> int: for stream in (sys.stdout, sys.stderr): if hasattr(stream, "reconfigure"): stream.reconfigure(encoding="utf-8", errors="backslashreplace") try: result = inventory() except (OSError, RuntimeError, ValueError, TypeError): print( json.dumps( { "execution_status": "configuration_error", "error_code": "network_interface_query_failed", "message": "本机 IPv4 网卡枚举失败;未执行设备发现,请检查 Windows 网络查询权限。", "device_discovery_executed": False, }, ensure_ascii=False, ) ) return 2 print(json.dumps(result, ensure_ascii=False)) return 0 if __name__ == "__main__": raise SystemExit(main())