我的服务器有超过2个网络接口。 即。原发性,BEN.nic,HB.nic等
使用以下行我从最后一个NIC获取IP:
for /f "tokens=2 delims=:" %%a in ('ipconfig ^| find "IP" ^| find "Address" ^| find /v "v6"') do (
set IPAddr=%%a
)
echo=%IPAddr%
我需要从特定的NIC名称中找到IP地址,即。 “BEN”
我也试过这个:
@echo on
setlocal ENABLEEXTENSIONS
setlocal EnableDelayedExpansion
set result=false
for /f "tokens=2 delims=:" %%a in ('ipconfig ^| "BEN.NIC" ^| find "IP Address"') do (
set IPAddr=%%a
)
echo %IPAddr%
它不起作用。
答案 0 :(得分:1)
下一个脚本应该可以工作:
@ECHO OFF >NUL
SETLOCAL enableextensions disabledelayedexpansion
set "nicFunName=wiredEthernet"
for /F "usebackq tokens=*" %%G in (
`wmic nic where "NetConnectionID='%nicFunName%'" get index /value^|find "="`) do (
rem echo G %%G
for /F "tokens=*" %%H in ("%%G") do (
rem echo H %%H
for /F "usebackq tokens=2 delims==" %%I in (
`wmic NICCONFIG where %%H get IPAddress /value`
) do (
rem echo I %%I
for /F "tokens=1,2 delims={,}" %%J in ("%%I") do (
echo IPv4=%%J IPv6=%%K
rem or without double quotes: echo IPv4=%%~J IPv6=%%~K
)
)
)
)
ENDLOCAL
goto :eof
for
循环
%%G
以Index=0
格式检索适用于下一个wmic
查询where
子句条件的网卡索引%%H
从0x0D
输出wmic
)
%%I
以IPAddress
格式{"<ipv4>","<ipv6>"}
%%J
拆分上一个输出您可以按如下方式添加最外层循环:
for %%m in (
"Primary.nic" "BEN.nic" "HB.nic"
) do (
rem all for... code here with
rem where "NetConnectionID='%%~m'"
rem or call a subroutine or call a batch etc.
)