我正在WinPE环境中编写脚本,我想找到我的PXE服务器并将其发送给我的MAC地址以检索配置脚本。
这是我必须处理的事情:
我希望脚本中的最后一行是:
curl -s -o %TEMP%/setup.cmd http://%PXE_IP%/cblr/svc/op/script/system/%MY_MAC%/?script=setup.cmd
我看到我从ipconfig /all
获得了所有必要的信息,但我不知道如何解析该输出。
例如我可以做
for /f "tokens=15 delims= " %%X in ('ipconfig /all ^| find "DHCP Server"') do echo %%X
这为我提供了每个适配器上DHCP服务器的IP地址。我可以确定哪个是正确的。但那又怎样?我需要该适配器的相应MAC地址。这些信息在输出中,但是我把它扔掉了find
。
答案 0 :(得分:1)
如果可能,我会使用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
MyComputer,10.0.0.1,13
MyComputer ,, 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字符结束。
答案 1 :(得分:1)
这是我的旧项目(2年半前的cca)中的脚本,通过解析wmic
输出来模拟ipconfig /ALL
结果。调整它以获得 DHCP服务器而不是 IPv4地址和 IPv6地址对你来说不应该是一项艰巨的任务......
@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!
)
答案 2 :(得分:0)
我不知道在您的情况下是否可以使用此解决方案来获取IP和MAC地址?
@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