我一直在尝试做一个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。
提前谢谢你!
答案 0 :(得分:1)
这可以通过单行完成。
ipconfig | find "192.168.1.101" >NUL && echo Match! || echo No match.
&&
运算符会在成功返回find
命令后进行求值。但是,如果find
失败(如果没有匹配则为true),则||
之后的内容会被评估。这基本上是以下的简写形式:
ipconfig | find "192.168.1.101" >NUL
if NOT ERRORLEVEL 1 (
echo Match!
) else (
echo No match.
)
使用find
的返回码(其%ERRORLEVEL%
)非常便于确定字符串是否存在于另一个字符串中。
有关条件执行的更多信息,read this。
编辑: 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!
)
)
)