Reputation: 1995
I am writing a script in a WinPE environment where I want to find my PXE server and send it my MAC address to retrieve provisioning scripts.
Here's what I have to deal with:
I want the final line in the script to be:
curl -s -o %TEMP%/setup.cmd http://%PXE_IP%/cblr/svc/op/script/system/%MY_MAC%/?script=setup.cmd
I see that I get all the necessary info from ipconfig /all
but I have no idea how to parse that output.
For example I can do
for /f "tokens=15 delims= " %%X in ('ipconfig /all ^| find "DHCP Server"') do echo %%X
This gives me the IP addresses for the DHCP servers on each adapter. I can determine which is correct. But then what? I need the corresponding MAC address for that adapter. That information is in the output, but I threw it away with my find
.
Upvotes: 3
Views: 925
Reputation: 18837
I don't know if this solution can be used or not in your case to get the IP and the MAC address ?
@echo off
Title Get IP and MAC Address
@for /f "delims=[] tokens=2" %%a in ('ping -4 -n 1 %ComputerName% ^| findstr [') do (
set "PXE_IP=%%a"
)
@For /f %%a in ('getmac /NH /FO Table') do (
@For /f %%b in ('echo %%a') do (
If /I NOT "%%b"=="N/A" (
Set "MY_MAC=%%b"
)
)
)
echo PXE_IP : %PXE_IP%
echo MAC Address : %MY_MAC%
echo(
echo curl -s -o %TEMP%/setup.cmd http://%PXE_IP%/cblr/svc/op/script/system/%MY_MAC%/?script=setup.cmd
pause>nul
Upvotes: 0
Reputation: 30123
Here's a script from my older project (cca 2½ years ago) simulating wmic
results via parsing ipconfig /ALL
output. Adapting it to get DHCP Server instead of IPv4 Address and IPv6 Address should not be a tough job for you…
@ECHO OFF
@SETLOCAL enableextensions enabledelayedexpansion
set "HostName="
set "NetConID="
set "IP_Addr4="
set "IP_Addr6="
set "MAC_Addr="
for /F "tokens=1* delims=:" %%G in ('ipconfig /ALL') do (
set "foo=%%~G"
if not "!foo:Host name=!"=="!foo!" (
for %%I in (%%~H) do if not "%%~I"=="" set "HostName=%%~I"
)
if "!foo:adapter=!"=="!foo!" (
if not "!foo:Physical Address=!"=="!foo!" (
for %%I in (%%~H) do if not "%%~I"=="" set "MAC_Addr=%%~I"
)
if not "!foo:IPv4 Address=!"=="!foo!" (
for %%I in (%%~H) do if not "%%~I"=="" set "IP_Addr4=%%~I"
set "IP_Addr4=!IP_Addr4:(preferred)=!"
)
if not "!foo:local IPv6 Address=!"=="!foo!" (
for %%I in (%%~H) do (
if not "%%~I"=="" (
for /F "delims=%%" %%p in ("%%~I") Do set "IP_Addr6=%%~p"
rem set "IP_Addr6=!IP_Addr6:(preferred)=!"
)
)
)
) else (
if not "!IP_Addr6!,!IP_Addr4!"=="," (
@echo #!HostName!,!NetConID!,{"!IP_Addr4!","!IP_Addr6!"},!MAC_Addr!
)
set "MAC_Addr="
set "IP_Addr4="
set "IP_Addr6="
set "NetConID=!foo:*adapter =!"
)
)
if not "!IP_Addr6!,!IP_Addr4!"=="," (
@echo =!HostName!,!NetConID!,{"!IP_Addr4!","!IP_Addr6!"},!MAC_Addr!
)
Upvotes: 1
Reputation: 82327
I would use wmic
instead of ipconfig, if possible on WinPE.
To get all active Interfaces
wmic nic where NetConnectionStatus=2 get InterfaceIndex, MACAddress,NetConnectionID /format:csv
MyComputer, 13, 40:47:40:4D:42:4C,Wireless Network Connection
MyComputer, 58, 00:50:56:C2:20:01,VMware Network Adapter VMnet1
And then you only need to combine this with the dhcp-server for each InterfaceIndex
wmic nicconfig get InterfaceIndex,DHCPServer /format:csv
MyComputer,10.0.0.1, 13
MyComputer,, 58
For fetching the data you use something like this
@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
Output:
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
Btw. I'm cheating a bit, as I always fetch one extra column that I doesn't need, but it's for avoiding the problem, that the last column ends with a CR character.
Upvotes: 1