我会使用wmic
而不是ipconfig,如果可能的话在WinPE上。
要获得所有活动接口
wmic nic where NetConnectionStatus=2 get InterfaceIndex, MACAddress,NetConnectionID /format:csv
我的电脑,13,40:47:40:4D:42:4C,无线网络连接
我的电脑,58,00:50:56:C2 :20:01,VMware的网络适配器VMnet1的
然后你只需要这与DHCP服务器结合起来,每个InterfaceIndex
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
输出:
网卡[ 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字符结尾。
来源
2017-03-08 15:08:06
jeb
使用'FOR/F'命令来分析'IPCONFIG'命令的输出。你可能想要将'IPCONFIG'的输出传送给'FIND'命令。 – Squashman
这样我可以得到例如DHCP服务器。但是一旦我找到正确的DHCP服务器,我怎么知道哪个MAC用于那个? AFAIK我无法获得仅用于特定适配器的ipconfig。 –