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



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

有关@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(:完成所有必需任务的代码,即将IP地址写入csv文件,如果更改,则每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%-----

相关内容

  • 没有找到相关文章

最新更新