0

我一直在尝试做一批执行 ipconfig 并获取 ip。然后它将 ip 匹配到一个值集。显示ip是否匹配。我发现的最接近的东西是在另一个帖子中

@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

当然这可能会做更多,但查看特定的适配器并查看我是否有 ip 以及是否有 ip 确保其设置的 ip。

先感谢您!

4

1 回答 1

1

这可以通过单线来完成。

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

&&操作员对命令的成功返回进行评估find。但是,如果find失败(如果没有匹配则为真),之后的东西||会被评估。这基本上是以下内容的简写形式:

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

使用find的返回码(它的%ERRORLEVEL%)对于确定一个字符串是否存在于另一个字符串中非常方便。

有关条件执行的更多信息,请阅读此


编辑: OP 评论说,“我有 1 个 USB wifi 适配器、内部 wifi 适配器和以太网端口,我希望每个人都检查特定的 ip....”这是一个可用于构建项目的基本框架。使用echo !Description! | find "wlan card identifier"上面演示的条件执行之类的东西来采取你想要的任何行动。快乐编码! :)

@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!
        )
    )
)
于 2014-12-02T20:18:02.693 回答