获取本地连接ip地址(通用版)
@echo off & setlocal enabledelayedexpansion
rem 如果系统中有route命令,优先采用方案1:
for /f “tokens=3,4” %%a in (\’route print^|find “0.0.0.0 0.0.0.0″\’) do (if not defined ip set “Gateway=%%a” & set “ip=%%b”)
for /f “tokens=2” %%a in (\’route print^|find “在链路上 %ip%”\’) do (if not defined mask set “mask=%%a” )
for /f “tokens=2” %%a in (\’route print^|findstr “在链路上.*%ip%”\’) do (if not defined mask set “mask=%%a” )
rem 如果系统中没有route命令,就采用方案2:
if defined ip goto :next
for /f “delims=[]” %%a in (\’ipconfig^|find /n “以太网:”\’) do if not defined n1 set /a n1=%%a+3
for /f “delims=[]” %%a in (\’ipconfig^|find /n “本地连接:”\’) do if not defined n1 set /a n1=%%a+3
set /a n2=%n1%+1
set /a n3=%n1%+2
for /f “skip=%n1% tokens=2 delims=:” %%a in (\’ipconfig\’) do if not defined ip set ip=%%a
for /f “skip=%n2% tokens=2 delims=:” %%a in (\’ipconfig\’) do if not defined mask set mask=%%a
for /f “skip=%n3% tokens=2 delims=:” %%a in (\’ipconfig\’) do if not defined gate set gate=%%a
set ip=%ip: =%
set mask=%mask: =%
set gate=%gate: =%
:next
echo IP地址是:%ip%
echo 子网掩码是:%mask%
echo 网关是:%gate%
pause