trying to make batch to match my current ip to a set ip

154 views Asked by At

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!

1

There are 1 answers

5
rojo On BEST ANSWER

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