如何用Python实现网线直连双机并获取MAC地址(带小窗口)及批量WiFi配置优化
问题1:Python实现双机网线直连并获取对方MAC地址
1. 先完成双机网络配置(Python辅助)
两台电脑直连需设置同一网段静态IP,可通过Python调用系统命令完成(需管理员权限):
Windows环境
import subprocess def set_static_ip(interface_name, ip, mask, gateway=None): cmd = f"netsh interface ip set address name=\"{interface_name}\" static {ip} {mask}" if gateway: cmd += f" {gateway}" subprocess.run(cmd, shell=True, check=True) # 调用示例:替换为你的网卡名称(如"以太网") # set_static_ip("以太网", "192.168.1.1", "255.255.255.0")
Linux/macOS环境
import subprocess def set_static_ip_linux(interface_name, ip, mask): subnet_prefix = mask.split('.')[-1] subprocess.run(f"sudo ip addr add {ip}/{subnet_prefix} dev {interface_name}", shell=True, check=True) subprocess.run(f"sudo ip link set {interface_name} up", shell=True, check=True) # 调用示例 # set_static_ip_linux("eth0", "192.168.1.2", "255.255.255.0")
2. 获取对方MAC地址
方法1:读取系统ARP缓存
import subprocess import re def get_mac_from_ip(target_ip): # 先ping目标IP更新ARP缓存 subprocess.run(f"ping -n 1 {target_ip}", shell=True, capture_output=True) # 读取ARP缓存 arp_result = subprocess.run("arp -a", shell=True, capture_output=True, text=True).stdout # 正则匹配MAC地址 mac_match = re.search(rf"{target_ip}\s+([0-9A-Fa-f:-]+)", arp_result) return mac_match.group(1).upper() if mac_match else None
方法2:用Scapy直接发送ARP请求
先安装依赖:pip install scapy
from scapy.all import ARP, Ether, srp def get_mac_scapy(target_ip): arp_req = ARP(pdst=target_ip) broadcast = Ether(dst="ff:ff:ff:ff:ff:ff") arp_broadcast = broadcast / arp_req answered = srp(arp_broadcast, timeout=1, verbose=False)[0] return answered[0][1].hwsrc.upper() if answered else None
3. 制作MAC查询小窗口(Tkinter)
import tkinter as tk from tkinter import messagebox def fetch_mac(): target_ip = ip_entry.get().strip() if not target_ip: messagebox.showerror("错误", "请输入目标IP") return mac = get_mac_from_ip(target_ip) # 可替换为get_mac_scapy if mac: mac_label.config(text=f"目标MAC地址: {mac}") else: messagebox.showwarning("提示", "无法获取MAC,检查网络连接或IP正确性") # 创建窗口 root = tk.Tk() root.title("MAC地址查询工具") root.geometry("320x160") # IP输入区域 ip_label = tk.Label(root, text="目标设备IP:") ip_label.pack(pady=8) ip_entry = tk.Entry(root, width=22) ip_entry.pack(pady=5) ip_entry.insert(0, "192.168.1.2") # 查询按钮 fetch_btn = tk.Button(root, text="获取MAC地址", command=fetch_mac) fetch_btn.pack(pady=12) # MAC显示区域 mac_label = tk.Label(root, text="目标MAC地址: ") mac_label.pack(pady=5) root.mainloop()
问题2:域内批量配置WiFi,避免手动输入MAC
方案1:组策略(GPO)批量推送
域环境下最便捷的方式,无需手动干预:
- 在测试机导出WiFi配置文件:
netsh wlan export profile name="你的WiFi名称" folder="C:\temp" key=clear,生成XML文件 - 编辑XML调整安全参数(如WPA2-Enterprise认证)
- 在域控制器的组策略管理中,创建新GPO,导航到计算机配置 > 策略 > Windows设置 > 网络连接 > WiFi策略(Windows Server 2012+支持),导入XML文件后部署到目标OU,设备会自动应用配置
方案2:PowerShell脚本批量配置(结合AD查询)
适合需要按MAC筛选设备的场景:
- 从AD导出目标设备MAC列表:
Get-ADComputer -Filter * -SearchBase "OU=Devices,DC=domain,DC=com" -Properties MACAddress | Select-Object Name, MACAddress | Export-Csv -Path "C:\device_macs.csv" -NoTypeInformation
- 批量推送WiFi配置:
$wifiProfilePath = "C:\temp\WiFi_Profile.xml" $deviceList = Import-Csv "C:\device_macs.csv" foreach ($device in $deviceList) { $macFormatted = $device.MACAddress.Replace(':','-') $adapter = Get-WmiObject -Class Win32_NetworkAdapter -Filter "MACAddress='$macFormatted'" ` -ComputerName $device.Name -ErrorAction SilentlyContinue if ($adapter) { netsh wlan add profile filename="$wifiProfilePath" interface="$($adapter.Name)" Write-Host "已为 $($device.Name) 配置WiFi" } }
执行需域管理员权限,目标设备需开启WMI访问
方案3:MDM工具自动化管理
若域内有Microsoft Intune、SCCM等MDM工具,可直接在控制台创建WiFi配置策略,基于设备组或MAC范围推送,完全自动化。
内容的提问来源于stack exchange,提问作者jidhu
相关产品推荐
相关产品推荐

