欢迎各位兄弟 发布技术文章
这里的技术是共享的
在 Windows 系统中,可以通过 Python 获取本机无线网卡的 MAC 地址。以下是几种实现方法:
getmac 库 # OK OK 有大用 有大大用
安装简单,直接获取无线网卡的 MAC 地址。
# 先用 dos 执行 # netsh wlan show interfaces 看看接口名称是不是 WLAN
安装依赖:
bashpip install getmac
wmi 模块(仅限 Windows) # 它非要 wifi 无线网连上网才行
pythonimport wmi def get_wireless_mac(): c = wmi.WMI() for interface in c.Win32_NetworkAdapterConfiguration(IPEnabled=True): if "Wireless" in interface.Description or "无线" in interface.Description: return interface.MACAddress return None mac = get_wireless_mac() print("无线网卡 MAC 地址:", mac)
安装依赖:
bashpip install wmi
subprocess 调用命令行
解析 ipconfig /all 或 getmac 命令的输出。
pythonimport re import subprocess def get_wireless_mac(): # 方法1: 通过 ipconfig 获取(需解析) output = subprocess.check_output("ipconfig /all", encoding="utf-8") match = re.search(r"无线.*?(\w\w-\w\w-\w\w-\w\w-\w\w-\w\w)", output, re.I) # 方法2: 通过 getmac 命令获取(更直接) if not match: output = subprocess.check_output("getmac", encoding="utf-8") match = re.search(r"(\w\w-\w\w-\w\w-\w\w-\w\w-\w\w).*无线", output, re.I) return match.group(1) if match else None mac = get_wireless_mac() print("无线网卡 MAC 地址:", mac)
权限问题:某些方法可能需要管理员权限。
多网卡情况:无线网卡可能有多个,需根据实际接口名称(如 Wi-Fi)筛选。
跨平台兼容性:getmac 库支持跨平台,而 wmi 仅限 Windows。
推荐优先使用 getmac 库,简单可靠。