本帖最后由 Echo11 于 2022-10-10 22:33 编辑
有个综合讨论贴的。
这个是我自己用的显示IP脚本之一。
里面有一点注释是多余的,是我的todo,后来懒得写了就没继续写。- @ECHO OFF & setlocal enabledelayedexpansion
- TITLE 显示所有已连接的网络连接信息 v3.2 by.Echo
- mode con: cols=80 lines=30
- CALL :get_NIC_info
- CALL :check_Gateway
- pause>nul
- EXIT /B 0
-
-
- :: _filter_ 可以是 NetEnabled,IPEnabled
- :get_NIC_info
- :: Default SETs
- ::SET "_from_=nic"
- ::SET "_filter_=NetEnabled"
- ::SET "_value_=TRUE"
-
- ::IF "%~1" NEQ "" SET "_filter_=%~1"
-
- ::IF "%_filter_%"=="IPEnabled" SET "_from_=nicconfig"
-
- SET "_i_=0"
-
- ECHO 所有已连接的网络连接信息(未插网线不会显示在下方):
- ECHO ---------------------------------------------------
- for /f "tokens=1,2 delims==" %%a in ('wmic nic where "NetEnabled='TRUE'" get Index^,MACAddress^,Description^,NetConnectionID /value') do (
- REM 此时,由于分隔符=不能过滤空行,此时的 %%a 含有回车或换行符等特殊符号,直接使用会导致代码混乱
- for /f "delims=" %%u in ("%%a") do for /f "delims=" %%v in ("%%b") do (
- REM 仅非空行会执行到这
- IF "%%u" NEQ "" SET "%%u=%%v"
- IF /i "%%u"=="Index" (
- for /f "tokens=1,2 delims==" %%c in ('wmic nicconfig where "Index=!Index!" get IPAddress^,IPSubnet^,DefaultIPGateway^,DNSServerSearchOrder /value') do (
- for /f "delims=" %%x in ("%%c") do for /f "delims=" %%y in ("%%d") do (
- IF "%%x" NEQ "" SET "%%x=%%y"& CALL :trim_IP %%x
- )
- )
- ) ELSE IF /i "%%u"=="NetConnectionID" (
- REM last value of one NIC here
- SET/a _i_+=1
- SET "NetConnectionID!_i_!=!NetConnectionID!"
- SET "Description!_i_!=!Description!"
- SET "MACAddress!_i_!=!MACAddress!"
- SET "IPAddress!_i_!=!IPAddress!"
- SET "IPSubnet!_i_!=!IPSubnet!"
- SET "DefaultIPGateway!_i_!=!DefaultIPGateway!"
- SET "DNSServerSearchOrder!_i_!=!DNSServerSearchOrder!"
- CALL :show_NIC_info !_i_!
- SET "NetConnectionID="
- SET "Description="
- SET "MACAddress="
- SET "IPAddress="
- SET "IPSubnet="
- SET "DefaultIPGateway="
- SET "DNSServerSearchOrder="
- )
- )
- )
- ECHO 已连接的网络连接数量: %_i_%
- GOTO:EOF
-
- :show_NIC_info
- :: 显示所有 NIC
- IF "%~1"=="" CALL :show_all_NIC
- IF "%~1" LEQ "!_i_!" IF "%~1" GEQ "1" CALL :show_one_NIC %~1
- GOTO:EOF
-
- :show_all_NIC
- IF !_i_!==0 GOTO:EOF
- for /L %%j in (1,1,!_i_!) do (
- CALL :show_one_NIC %%j
- )
- GOTO:EOF
-
- :show_one_NIC
- ECHO No.%1 : !NetConnectionID%1!
- ECHO 描述 : !Description%1!
- ECHO MAC 物理地址 : !MACAddress%1!
- ECHO IPv4 地址 : !IPAddress%1!
- ECHO 子网掩码 : !IPSubnet%1!
- ECHO 默认网关 : !DefaultIPGateway%1!
- ECHO DNS 服务器 : !DNSServerSearchOrder%1!
- ECHO ----------------
- GOTO:EOF
-
- :check_Gateway
- IF !_i_!==0 GOTO:EOF
- ECHO -----------------------
- IF !_i_!==1 ECHO 若需要连续ping网关检测网络...请按任意键开始...&pause>nul&CALL :ping_Gateway !_i_! &GOTO:EOF
- SET /P choice=若需要连续ping网关检测网络...请输入网络连接No.序号:
- CALL :ping_GateWay %choice%
- GOTO:EOF
-
- :ping_GateWay
- IF "%~1" GTR "!_i_!" GOTO:EOF
- IF "%~1" LSS "1" GOTO:EOF
- IF "!DefaultIPGateway%~1!"=="" ECHO 网络连接:!NetConnectionID%~1! 未设置网关&GOTO:EOF
- ping !DefaultIPGateway%~1! -t
- GOTO:EOF
-
- :trim_IP
- :: %1 传入存有IP的变量名,而不是值
- :: 分别去掉"{}
- :: IP地址只取IPv4
- :: 子网掩码只取掩码
- (set %1=!%1:^"=!&set %1=!%1:{=!&set %1=!%1:}=!)
- IF /i "%1"=="IPAddress" for /f "delims=," %%a in ("!%1!") do set "%1=%%a"
- IF /i "%1"=="IPSubnet" for /f "delims=," %%a in ("!%1!") do set "%1=%%a"
- GOTO:EOF
复制代码
|