作为 CMD 变量的特定以太网的 IP 地址

IP Address of Specific Ethernet as CMD Variable

我需要每 10 分钟检查一次我的 IP 地址并将其写入 csv 文件,如果它已更改。但是,我有几个网卡在使用中。如何在 CMD 中使用其 MAC 地址获取特定卡的 IP 地址?

请参阅下文以修改对 another question by @mousio 的回复。但是它对我不起作用!

@echo off
setlocal enabledelayedexpansion
set "MAC1=Physical Address"
set "MAC2=11-11-11-11-11-11"
set MACfound=false
for /f "usebackq tokens=1-2 delims=:" %%f in (`ipconfig /all`) do (
    set "item1=%%f"
    set "item2=%%g"
    if /i "!item1!"=="!MAC1!" if "!item2!"=="!MAC2!" (
        set MACfound=true
    ) else if not "!item1!"=="!item:IPv4 Address=!" if "!MACfound!"=="true" (
        echo Your IP Address is: %%g
        set MACfound=false
    )
)

有关 ipconfig /all 的部分响应,请参见下文

Ethernet adapter Ethernet 3:

   Connection-specific DNS Suffix  . : xyz.xyz.com
   Description . . . . . . . . . . . : Intel(R) 82579LM Gigabit Network Connection
   Physical Address. . . . . . . . . : 11-11-11-11-11-11
   DHCP Enabled. . . . . . . . . . . : Yes
   Autoconfiguration Enabled . . . . : Yes
   Link-local IPv6 Address . . . . . : 1111::1111:1111:1111:111111(Preferred)
   IPv4 Address. . . . . . . . . . . : 111.11.11.11(Preferred)
   Subnet Mask . . . . . . . . . . . : 111.111.1.1
   Lease Obtained. . . . . . . . . . : Thursday, July 25, 2019 9:51:30 AM
   Lease Expires . . . . . . . . . . : Monday, August 26, 2019 12:33:23 PM
   Default Gateway . . . . . . . . . : 111.11.1.1
   DHCP Server . . . . . . . . . . . : 111.11.11.11
   DHCPv6 IAID . . . . . . . . . . . : 111111111
   DHCPv6 Client DUID. . . . . . . . : 11-11-11-11-11-11-11-11-11-11-11-11-11-11
   DNS Servers . . . . . . . . . . . : 111.11.11.11
                                       111.11.11.11
   NetBIOS over Tcpip. . . . . . . . : Enabled

IPconfig 很难解析,因为所需的信息分布在多行中。使用正确的工具。我推荐wmic

for /f "tokens=2 delims={}" %%a in ('wmic nicconfig where MACAddress^="11:11:11:11:11:11" get IPAddress /value') do echo %%~a

注意:MAC 地址的格式在 wmic 中不同(冒号而不是破折号)。不要忘记转义 =.

编辑: 分隔 IPv4 和 IPv6 地址,只需将字符串与另一个 for 循环分开:

@echo off
setlocal enabledelayedexpansion
for /f "tokens=2 delims={}" %%a in ('wmic nicconfig where MACAddress^="11:11:11:11:11:11" get IPAddress /value') do set "adresses=%%a"
echo All Addresses: %adresses%
for %%a in (%adresses%) do (
  echo %%~a|find "." >nul && set "ip4=!ip4!,%%~a
  echo %%~a|find ":" >nul && set "ip6=!ip6!,%%~a
)
echo IPv4-Address(es): %ip4:~1%
echo IPv6-Address(es): %ip6:~1%

编辑(来自 Mosy):完成所有必需任务的代码,即在 csv 文件中写入 ip 地址,如果它发生变化则每 10 分钟更新一次:

基本上,同一个路径下会有两个批处理文件,第一个叫做ip_main.bat,包含:

@echo off
echo -- IP ADDRESS UPDATER, PLEASE DO NOT CLOSE! --
set parent=%~dp0%
CD "%parent%"
setlocal enabledelayedexpansion
for /f "tokens=2 delims={}" %%a in ('wmic nicconfig where MACAddress^="11:11:11:11:11:11" get IPAddress /value') do set "adresses=%%a"

for %%a in (%adresses%) do (
  echo %%~a|find "." >nul && set "ip4=!ip4!,%%~a
  echo %%~a|find ":" >nul && set "ip6=!ip6!,%%~a
)
set ip4_old=%ip4:~1%
set ip6_old=%ip6:~1%
set "ip4=%ip4*=%"
set "ip6=%ip6*=%"

call ip_writer > ip_file.csv

:loop
for /f "tokens=2 delims={}" %%a in ('wmic nicconfig where MACAddress^="11:11:11:11:11:11" get IPAddress /value') do set "adresses=%%a"

for %%a in (%adresses%) do (
  echo %%~a|find "." >nul && set "ip4=!ip4!,%%~a
  echo %%~a|find ":" >nul && set "ip6=!ip6!,%%~a
)
set ip4_new=%ip4:~1%
set ip6_new=%ip6:~1%
set "ip4=%ip4*=%"
set "ip6=%ip6*=%"

if not "%ip4_new%"=="%ip4_old%" (
    call ip_writer > ip_file.csv
    set ip4_old=%ip4_new%
)

set "ip4_new=%ip4*=%"    

timeout 600 /nobreak > nul
goto loop

第二批文件名为 ip_writer.bat 并包含

@echo off
set parent=%~dp0%
CD "%parent%"
setlocal enabledelayedexpansion
for /f "tokens=2 delims={}" %%a in ('wmic nicconfig where MACAddress^="11:11:11:11:11:11" get IPAddress /value') do set "adresses=%%a"
rem echo All Addresses: %adresses%
for %%a in (%adresses%) do (
  echo %%~a|find "." >nul && set "ip4=!ip4!,%%~a
  echo %%~a|find ":" >nul && set "ip6=!ip6!,%%~a
)
set ip4=%ip4:~1%
set ip6=%ip6:~1%

echo ip4_address
echo %ip4%

这是另一种方法。

FOR /F "tokens=1-2" %%A IN ('powershell -NoLogo -NoProfile -Command ^
    "Get-CimInstance -ClassName Win32_NetworkAdapterConfiguration |" ^
    "Where-Object { $_.MACAddress -match '11:11:11:11:11:11' } |" ^
    "ForEach-Object { '{0} {1}' -f $_.IPAddress[0], $_.IPAddress[1] }"') DO (
    SET "IPV4ADDR=%%~A"
    SET "IPV6ADDR=%%~B"
)
ECHO IPV4ADDR is %IPV4ADDR%
ECHO IPV6ADDR is %IPV6ADDR%

从Mac

获取本地IP
    setlocal enabledelayedexpansion
    set Mac=hy80::c1sd:6452:4t5:d574
    set "i=0"
    for /f "tokens=2 delims={} skip=1" %%i in ('wmic nicconfig get IPAddress /value' )                 do (
    echo %%i|find "." >nul && set "ip4[!i!]=%%i
    set /a "i+=1"
    )
    set /a "i-=1"
    for /l %%a in (0,1,%i%) do (
        set ip4ip6=!ip4[%%a]!"
        for /f tokens^=1^,2^,3^ delims^=^" %%a in (!ip4ip6!) do (
            set temp=%%a
            set tempMac=%%c
        )
        if "!Mac!" equ "!tempMac!" (
            set localMac=!tempMac!
            set localIp=!temp!
        )
    )
    echo ----%localIp%-----
    echo ----%localMac%-----

从本地Ip获取Mac

    setlocal enabledelayedexpansion
    set "i=0"
    for /f "tokens=2 delims={} skip=1" %%i in ('wmic nicconfig get IPAddress /value' )         do (
    echo %%i|find "." >nul && set "ip4[!i!]=%%i
    set /a "i+=1"
    )
    set /a "i-=1"
    for /l %%a in (0,1,%i%) do (
        set ip4ip6=!ip4[%%a]!"
        for /f tokens^=1^,2^,3^ delims^=^" %%a in (!ip4ip6!) do (
            set temp=%%a
            set tempMac=%%c
        )
        if "!temp:~0,12!" equ "192.168.137." (
            set localIp=!temp!
            set localMac=!tempMac!
        ) else if "!temp:~0,8!" equ "127.0.0." (
            set localIp=!temp!
            set localMac=!tempMac!
        ) else if "!temp:~0,12!" equ "Some.else.IP." (
            set localIp=!temp!
            set localMac=!tempMac!
        )
    )
    echo ----%localIp%-----
    echo ----%localMac%-----