本帖最后由 aa77dd@163.com 于 2015-8-13 18:31 编辑
调用 WMIC 获取信息, 而不依赖于连接的名称(如 本地连接 n 无线网络连接 n 等等)进行文本解析, 即使连接的名称被任意更改了, 一样能正确输出各个 NIC 接口的状态信息.
是从一个更长的代码改写的, 实测可以获得你要的输出, 当然代码中还有冗余的部分, 我不想继续改了, 楼主有兴趣就自行给代码瘦身吧- @echo off & setlocal enabledelayedexpansion
- echo 扫描 NIC 接口...
- (call :getConnectionName nicCnt) & cls
- if !nicCnt! leq 0 ( echo 没找到在运行的 NIC 接口 & exit )
- call :viewNIC
- pause
- exit
-
-
- :viewNIC
- >NIC.TXT (
-
- for /f "tokens=1,2 delims==" %%a in ('wmic nicconfig where "ipenabled='true'" get /value') do (
- if "%%a" gtr "" set "%%a=%%b"
-
- if /i "%%a"=="Index" (
- set /a "%%a=%%b"
- for /f "tokens=1,2 delims==" %%x in ('wmic nic where "index=!index!" get /value') do (
- if "%%x" gtr "" set "%%x=%%y"
- if /i "%%x"=="NetConnectionStatus" for %%z in (%%y) do set "%%x=!%%x%%z!"
- if /i "%%x"=="ConfigManagerErrorCode" for %%z in (%%y) do set "%%x=!%%x%%z!"
- )
- )
-
- if /i "%%a"=="WINSSecondaryServer" (
- call :getIP IPAddress& call :getIP IPSubnet& call :getIP DefaultIPGateway& call :getIP DNSServerSearchOrder
-
- for %%n in (Description NetConnectionID IPAddress) do (
- for /f "delims=" %%a in ("!%%n!") do set "%%n=%%~a"
- )
-
- REM 去掉 ipv6 的 ip 地址
- for /f "delims=," %%a in ("!IPAddress!") do set "IPAddress=%%a"
-
- for %%n in ("!NetConnectionID!") do (
- if not "%%~n" gtr "" (
- echo;!Description!: !IPAddress!
- ) else (
- echo;!NetConnectionID!: !IPAddress!
- )
- )
- )
- )
-
- )
- start NIC.TXT
- exit /b
-
- :getIP
- (set %1=!%1:^"=!&set %1=!%1:{=!&set %1=!%1:}=!)
- exit /b
-
- :getEnum
- for /f "tokens=1,2* delims= " %%a in (%1) do (
- if /i "!getValue!"=="Y" set "%2%%a=%%c"
- if /i "%%a"==":%2" set getValue=Y
- if /i "%%a"==":end:%2" set getValue=N
- )
- exit /b
-
-
- :getConnectionName nicCnt
- set /a %1=0
- (
- echo IP enabled NIC:
- for /f "tokens=1,2 delims==" %%a in ('wmic nicconfig where "ipenabled='true'" get Index^,MACAddress /value') do (
- set MACAddress=
- for /f "delims=" %%u in ("%%a") do for /f "delims=" %%v in ("%%b") do (
- if "%%u" neq "" set "%%u=%%v"
- )
- if /i "%%a"=="Index" (
- set NetConnectionID=
- for /f "tokens=1,2 delims==" %%x in ('wmic nic where "index=!index!" get NetConnectionID^,Description /value') do (
- for /f "delims=" %%u in ("%%x") do for /f "delims=" %%v in ("%%y") do (
- if "%%u" neq "" set "%%u=%%v"
- )
- )
- ) else if /i "%%a"=="MACAddress" (
- set /a %1+=1
- set /a "NIC_Index!%1!=index"
-
- if "!NetConnectionID!"=="" (set "NCID!%1!=!Description!"
- ) else set "NCID!%1!=!NetConnectionID!"
-
- set "MACAddress!%1!=!MACAddress!"
- for %%k in (!%1!) do (
- echo !%1!. !NCID%%k!
- echo MAC: !MACAddress%%k!
- )
- )
- )
- )
- exit /b
复制代码
|