如果可能的话,我会在 WinPE 上使用wmic
而不是 ipconfig。
获取所有活动接口
wmic nic where NetConnectionStatus=2 get InterfaceIndex, MACAddress,NetConnectionID /format:csv
MyComputer, 13, 40:47:40:4D:42:4C,无线网络连接
MyComputer, 58, 00:50:56:C2:20:01,VMware 网络适配器 VMnet1
然后你只需要将它与每个 InterfaceIndex 的 dhcp-server 结合起来
wmic nicconfig get InterfaceIndex,DHCPServer /format:csv
我的电脑,10.0.0.1, 13我的
电脑,, 58
为了获取数据,您使用类似这样的东西
@echo off
setlocal EnableDelayedExpansion
REM *** Delete all previous variables beginning with "nic_"
for /F "delims==" %%V in ('set nic[ 2^> nul') do set "%%V="
for /F "skip=2 tokens=1-4* delims=," %%1 in ('"wmic nic where NetConnectionStatus=2 get InterfaceIndex, MACAddress,NetConnectionID,Status /format:csv"') do (
echo DEBUG: "%%4",%%2,%%3
set "nic[%%2].mac=%%3"
)
for /F "skip=2 delims=" %%L in ('"wmic nicconfig get InterfaceIndex,DHCPServer,IPAddress /format:csv"') do (
set "line=%%L"
set "line=""!line:,=,"!"" --- Pump up the csv line with quotes to avoid empty columns col1,,col2 transformed to "col1","","col3"
for /F "tokens=1-4* delims=," %%1 in ("!line!") do (
if "%%~2" NEQ "" (
set nic[%%~3].dhcpServer=%%~2
)
)
)
set nic
输出:
nic[13].dhcpServer=10.0.0.1
nic[13].mac=40:47:40:4D:42:4C
nic[58].mac=00:50:56:C2:20:01
顺便提一句。我有点作弊,因为我总是获取一个不需要的额外列,但为了避免问题,最后一列以 CR 字符结尾。