获取本地连接ip地址(通用版)
Posted liuzhaoyzz
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了获取本地连接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 ( set "gate=%%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
以上是关于获取本地连接ip地址(通用版)的主要内容,如果未能解决你的问题,请参考以下文章
无法修改本地连接IP地址,改了以后自动变回来,不知道是啥原因,来高手