1

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
Mosy
  • 175
  • 1
  • 1
  • 9

3 Answers3

2

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%
Stephan
  • 53,940
  • 10
  • 58
  • 91
  • @stephen I did this but I get bot IPv4 and (I guess) IPv6 for example 111.11.11.11","aa11::a111:1aaa:aaa8:a1a1 How can I can get these into two separate variables? – Mosy Jul 27 '19 at 10:44
  • thank you very much that's what I wanted. I edited your answer to add all the other required tasks in case someone else wants to do the same thing and needs some hint. – Mosy Jul 28 '19 at 08:07
1

Here is another way to do it.

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%
lit
  • 14,456
  • 10
  • 65
  • 119
0

Get local Ip from Mac

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

Get Mac from local Ip

    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%-----
Garric
  • 591
  • 3
  • 10