我會使用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。 –