0

i have been trying to do a batch that does an ipconfig and gets the ip. it then matches the ip to a value set. displaying if the ip matches or not. the closest thing i found was in another post which si

@echo off

rem --- complete adapter name to find without the ending ":" ---
set adapter=Wireless LAN adapter Wireless Network Connection

rem --- token under an adapter to extract IP address from ---
set IPAddrToken=IPv4 Address

rem --- token under an adapter to extract IP address from ---
set matchipaddress=192.168.1.101

setlocal enableextensions enabledelayedexpansion
set adapterfound=false
set emptylines=0
set ipaddress=

for /f "usebackq tokens=1-3 delims=:" %%e in (`ipconfig ^| findstr /n "^"`) do (

    set "item=%%f"

    if /i "!item!"=="!adapter!" (
        set adapterfound=true
        set emptylines=0
    ) else if not "!item!"=="" if not "!item!"=="!item:%IPAddrToken%=!" if "!adapterfound!"=="true" (
        @rem "!item:%IPAddrToken%=!" --> item with "IPv4 Address" removed
        set ipaddress=%%g
        goto :result
    )
    if "%%f-%%g-!adapterfound!-!emptylines!"=="--true-1" (
        @rem 2nd blank line after adapter found
        goto :result
    )
    if "%%f-%%g-!adapterfound!-!emptylines!"=="--true-0" (
        @rem 1st blank line after adapter found
        set emptylines=1
    )
)

endlocal

:result
    echo %adapter%
    echo.
    if not "%ipaddress%"=="" (
        echo    %IPAddrToken% =%ipaddress%
    ) else (
        if "%adapterfound%"=="true" (
            echo    %IPAddrToken% Not Found
        ) else (
            echo    Adapter Not Found
        )
    )

ECHO.    

PAUSE

sure this might do a bit more but looking into a specific adapter and seeing if i have an ip or not and if i do have an ip make sure its the set ip.

thank you in advance!

droopie
  • 441
  • 3
  • 12
  • i just realized that what i might need instead of ip check is checking the gateway ip to see if its changed, not the ip. also in addition to that, since i have 1 usb wifi adapter, internal wifi adapter, and ethernet port, i would love to have each one check for the specific ip. the reason for this is because i have multiple networks, school, work, home, and mobile that i need programs either started or killed based on adapter and gateway (since ip doesnt seem to lookup public) and based on that adapter i might be using a vpn. thats why i am looking for changes of gateway ips in the adapters. – droopie Dec 02 '14 at 22:39

1 Answers1

1

This can be done with a one-liner.

ipconfig | find "192.168.1.101" >NUL && echo Match! || echo No match.

The && operator evaluates upon a successful return of the find command. However, if find fails (which is true if no matches), the stuff after || gets evaluated instead. This is basically a shorthand form of the following:

ipconfig | find "192.168.1.101" >NUL
if NOT ERRORLEVEL 1 (
    echo Match!
) else (
    echo No match.
)

Using find's return code (its %ERRORLEVEL%) is very handy for determining whether a string exists within another string.

For more info on conditional execution, read this.


Edit: O.P. commented, "I have 1 usb wifi adapter, internal wifi adapter, and ethernet port, i would love to have each one check for the specific ip...." Here's a basic skeleton you can use to build your project. Use something like echo !Description! | find "wlan card identifier" with conditional execution demonstrated above to take whatever action you wish. Happy coding! :)

@echo off
setlocal enabledelayedexpansion

set "home=10.0.0"
set "school=192.168"
set "work=172.16"

for /f "skip=1 tokens=1* delims={}" %%I in ('wmic nicconfig where "ipenabled=true" get DefaultIPGateway^, Description') do (
    set "IP=%%~I"

    rem :: make sure !IP! contains numbers before continuing
    echo !IP! | findstr "[0-9]" >NUL && (

        rem :: trim left from %%J
        for /f "tokens=* delims= " %%x in ("%%~J") do set Description=%%x

        if "!IP:%home%=!" neq "!IP!" (
            echo Connected at home on !Description!
        ) else if "!IP:%school%=!" neq "!IP!" (
            echo Connected at school on !Description!
        ) else if "!IP:%work%=!" neq "!IP!" (
            echo Connected at work on !Description!
        ) else (
            echo Unknown connection on !Description!
        )
    )
)
rojo
  • 24,000
  • 5
  • 55
  • 101
  • that works great but how do i enable it for a specific adapter? i have like 3 adapters and i want certain adapters with a specific ip to kill processes – droopie Dec 02 '14 at 22:12
  • You could use `wmic nicconfig` instead of `ipconfig`. I'll post an example next time I get to a computer unless someone else beats me to it. – rojo Dec 02 '14 at 22:22
  • i just realized that what i might need instead of ip check is checking the gateway ip to see if its changed, not the ip. also in addition to that, since i have 1 usb wifi adapter, internal wifi adapter, and ethernet port, i would love to have each one check for the specific ip. the reason for this is because i have multiple networks, school, work, home, and mobile that i need programs either started or killed based on adapter and gateway (since ip doesnt seem to lookup public) and based on that adapter i might be using a vpn. thats why i am looking for changes of gateway ips in the adapters. – droopie Dec 02 '14 at 22:37
  • @droopie See edits above. If this answer was helpful, please consider marking it as accepted. [See this page](http://meta.stackexchange.com/q/5234) for an explanation of why this is important. – rojo Dec 03 '14 at 02:20
  • thanks rojo. so far it is able to display which adapter is connected to what ip. the only issue is when connected to the vpn i get Unknown connection on Connected at work on TAP-Win32 Adapter V9 ill still test it out and post back! – droopie Dec 03 '14 at 15:02
  • thanks! i voted your answer since it does fit my needs. i removed the end ) else ( so it doesnt display that message. the only thing i wish it did is that first, get the adapter connected and THEN do ip check. this would give me an extra layer of checks. but this will do just fine! again, thank you rojo! – droopie Dec 05 '14 at 18:58