码迷,mamicode.com
首页 > 其他好文 > 详细

获取本地连接ip地址(通用版)

时间:2019-12-13 20:04:33      阅读:311      评论:0      收藏:0      [点我收藏+]

标签:命令   set   local   dex   ansi   enable   ipc   etl   off   

@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地址(通用版)

标签:命令   set   local   dex   ansi   enable   ipc   etl   off   

原文地址:https://www.cnblogs.com/liuzhaoyzz/p/12036827.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!