windowscmdipconfig

IP Address of Specific Ethernet as CMD Variable


I need to check my ip address every 10 minutes and write it to a csv file, if it has changed. However, I have several network cards in use. How can I get IP address of a specific card in CMD using its MAC address?

See below for modification of response to another question by @mousio . It didn't work for me though!

@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
    )
)

See below for part of response for 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

Solution

  • IPconfig is hard to parse because the needed information is distributed over several lines. Use the right tools. I recommend wmic:

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

    Note: the format of the MAC address is different in wmic (colons instead of dashes). Don't forget to escape the =.

    Edit: to separate IPv4 and IPv6 Addresses, just split the string with another for loop:

    @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%
    

    Edit (by Mosy): Code to accomplish all required tasks, i.e. write ip address in a csv file and update it every 10 minutes if it has changed:

    Basically, there will be two batch files in the same path, the first one is called ip_main.bat and contains:

    @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
    

    The second batch file is called ip_writer.bat and contains

    @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%