2014-12-02 40 views
0

我一直在嘗試做一個ipconfig並獲取IP的批處理。然後它將ip匹配到一個值集。顯示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

我意識到我可能需要而不是IP檢查檢查網關IP,看看它的變化,而不是IP。 此外,因爲我有1個USB適配器,內部WiFi適配器和以太網端口,我很想有每一個檢查特定的IP。 原因是因爲我有多個網絡,學校,工作,家庭和移動,我需要程序啓動或殺死基於適配器和網關(因爲IP似乎沒有查找公開),並基於該適配器我可能使用vpn。這就是爲什麼我正在尋找適配器中網關ips的變化。 – droopie 2014-12-02 22:39:45

回答

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

這很好,但我如何啓用它的特定適配器?我有3個適配器,我想用特定的IP來殺死進程的某些適配器 – droopie 2014-12-02 22:12:50

+0

您可以使用'wmic nicconfig'而不是'ipconfig'。我會在下次看電腦時發表一個例子,除非有人打我。 – rojo 2014-12-02 22:22:00

+0

我剛剛意識到,我可能需要而不是IP檢查是檢查網關IP,看看它的變化,而不是IP。 此外,因爲我有1個USB適配器,內部WiFi適配器和以太網端口,我很想有每一個檢查特定的IP。 原因是因爲我有多個網絡,學校,工作,家庭和移動,我需要程序啓動或殺死基於適配器和網關(因爲IP似乎沒有查找公開),並基於該適配器我可能使用vpn。這就是爲什麼我正在尋找適配器中網關ips的變化。 – droopie 2014-12-02 22:37:56