- @echo off
- REM verify if any ipv4 address falls in a special subnet
- setlocal EnableDelayedExpansion
- set "subnet1=192.168.0.25-192.168.0.78"
- set "subnet2=192.168.3.52-192.168.3.90"
- set "subnet3=192.168.5.20-192.168.5.55"
-
- for /f "tokens=3" %%A in ('netsh interface ipv4 show addresses^|find /i "IP"^|find /v "127.0.0.1"') do (
- set IPv4=%%A
- call :IPv4ToInt32 "%%A" "IPv4my"
- for /f "tokens=1-3 delims==-" %%B in ('set subnet') do (
- call :IPv4ToInt32 "%%C" "IPv4lo"
- call :IPv4ToInt32 "%%D" "IPv4hi"
- if !IPv4my! geq !IPv4lo! if !IPv4my! leq !IPv4hi! (
- set fallsubnet=%%B
- goto subnetproc
- )
- )
- )
- goto end
-
- :subnetproc
- echo.Your IPv4 address "%IPv4%" is in "%fallsubnet%=!%fallsubnet%!"
- REM more tasks
- goto end
-
- :end
- endlocal
- pause
- exit /b
-
- :IPv4ToInt32 IPv4 outvar
- set Int32=0
- for /f "tokens=1-4 delims=." %%A in ("%~1") do (
- set /a "Int32=((%%A*256+%%B)*256+%%C)*256+%%D"
- )
- if not "%~2"=="" set %~2=%Int32%
- exit /b
复制代码
|