獲取本地連接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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM