Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get MAC and DHCP server from ipconfig

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:

  • The server will have multiple network adapters. Only one is connected to my PXE server.
  • The PXE server is always also the DHCP server on that network.
  • There may be other DHCP servers connected to the other NICs.
  • The PXE server is also listening on port 80.
  • cURL is available in the WinPE image.

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.

like image 325
Christoffer Reijer Avatar asked Dec 22 '25 07:12

Christoffer Reijer


2 Answers

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.

like image 166
jeb Avatar answered Dec 24 '25 11:12

jeb


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!
)
like image 34
JosefZ Avatar answered Dec 24 '25 10:12

JosefZ



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!