|
本帖最后由 liuzhaoyzz 于 2022-10-9 10:48 编辑
两个方案保底,一个不行自动用另一个,如果系统中精简了findstr命令,尝试用find命令。未测试一个网卡绑定多个ip的情况。
edit:已更换为plusv方案,感谢分享 获取 内网 IP、子网掩码、网关的 EXE 文件 - 脚本讨论区 - 无忧启动论坛 - Powered by Discuz! http://wuyou.net/forum.php?mod=viewthread&tid=418506
@echo off & setlocal enabledelayedexpansion
rem 获取ip、网关、子网掩码:
rem 如果系统中有route命令,优先采用方案1:
for /f "tokens=3,4" %%a in ('route print^|findstr /RC:"0.0.0.0 0.0.0.0.*0$"') do (if not defined ip set "Gateway=%%a" & set "ip=%%b")
for /f "tokens=3,4" %%a in ('route print^|find "0.0.0.0 0.0.0.0"^|find " 10"') do (if not defined ip set "Gateway=%%a" & set "ip=%%b")
for /f "tokens=3,4" %%a in ('route print^|find "0.0.0.0 0.0.0.0"^|find " 20"') do (if not defined ip set "Gateway=%%a" & set "ip=%%b")
for /f "tokens=2" %%a in ('route print^|findstr "在链路上.*%ip%"') do (if not defined mask set "mask=%%a" )
for /f "tokens=2" %%a in ('route print^|find "在链路上 %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 Gateway set Gateway=%%a
set ip=%ip: =%
set mask=%mask: =%
set Gateway=%Gateway: =%
:next
echo IP地址是:%ip%
rem for /f "tokens=1,2,3 delims=." %%a in ("%ip%") do (if not defined IP_Pool set IP_Pool=%%a.%%b.%%c.2)
rem echo IP起始地址是:%IP_Pool%
echo 子网掩码是:%mask%
echo 网关是:%Gateway%
pause
exit /b
|
评分
-
查看全部评分
|