显示所有已连接的网络连接信息

@ECHO OFF & setlocal enabledelayedexpansion
TITLE 显示所有已连接的网络连接信息
mode con: cols=80 lines=30
CALL :get_NIC_info
pause>nul
EXIT /B 0


:get_NIC_info
SET "_i_=0"


ECHO 所有已连接的网络连接信息(未插网线不会显示在下方):
ECHO ---------------------------------------------------
for /f "tokens=1,2 delims==" %%a in ('wmic nic where "NetEnabled='TRUE'" get Index^,MACAddress^,NetConnectionID /value') do (
    for /f "delims=" %%u in ("%%a") do for /f "delims=" %%v in ("%%b") do (
        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
            IF defined DefaultIPGateway (
                IF defined DNSServerSearchOrder (
                    SET/a _i_+=1
                    ECHO    No.!_i_!         : !NetConnectionID!
                    ECHO    MAC 物理地址 : !MACAddress!
                    ECHO    IPv4 地址    : !IPAddress!
                    ECHO    子网掩码     : !IPSubnet!
                    ECHO    默认网关     : !DefaultIPGateway!         
                    ECHO    DNS 服务器   : !DNSServerSearchOrder!        
                    ECHO ----------------
                    SET "MACAddress="
                    SET "NetConnectionID="
                    SET "IPAddress="
                    SET "IPSubnet="
                    SET "DefaultIPGateway="
                    SET "DNSServerSearchOrder="
                )
            )
        )
    )
)
ECHO 已连接的网络连接数量: %_i_%
GOTO:EOF

:trim_IP
(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

注意要将bat文件格式设置为 ANSI

用于显示所有已连接的网络连接信息。它通过wmic命令获取网络接口信息,然后逐个处理这些信息以显示每个连接的详细信息。

一个名为trim_IP函数,它用于处理IP地址和子网掩码。这个函数接受一个参数(IP地址或子网掩码),然后去除引号,并分割值以进行进一步处理。

你可能感兴趣的:(脚本,batch)