标题: [文本处理] [以解决]批处理如何提取多个网络连接的ip地址 [打印本页]
作者: Linux9253 时间: 2015-8-13 12:37 标题: [以解决]批处理如何提取多个网络连接的ip地址
本帖最后由 pcl_test 于 2016-10-14 01:24 编辑
多个网卡接口。提取对应的
Microsoft Windows [版本 5.2.3790]
(C) 版权所有 1985-2003 Microsoft Corp.
C:\Documents and Settings\Administrator>ipconfig
Windows IP Configuration
Ethernet adapter 本地连接 2:
Connection-specific DNS Suffix . :
IP Address. . . . . . . . . . . . : 10.131.80.51
Subnet Mask . . . . . . . . . . . : 255.255.255.128
Default Gateway . . . . . . . . . :
Ethernet adapter 本地连接 3:
Connection-specific DNS Suffix . :
IP Address. . . . . . . . . . . . : 12.21.1.91
Subnet Mask . . . . . . . . . . . : 255.255.255.192
Default Gateway . . . . . . . . . : 12.21.1.126
C:\Documents and Settings\Administrator>
取出
本地连接 2: 10.131.80.51
本地连接 3: 12.21.1.91
它们的对应关系
作者: aa77dd@163.com 时间: 2015-8-13 16:07
本帖最后由 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
复制代码
作者: Linux9253 时间: 2015-8-13 17:09
本帖最后由 Linux9253 于 2015-8-13 17:22 编辑
回复 2# aa77dd@163.com
非常感谢、解决了我的问题、代码很复杂、虽然我没有看懂。但写这么多代码肯定很费脑子和精力、所以、谢谢哈
不只能否把后面的ipv6去掉?
作者: aa77dd@163.com 时间: 2015-8-13 18:07
本帖最后由 aa77dd@163.com 于 2015-8-13 18:32 编辑
回复 3# Linux9253
还是看 2 楼, 我多加了一行, 去掉 IPV6 地址用的
作者: Linux9253 时间: 2015-8-14 10:58
回复 4# aa77dd@163.com
非常感谢、麻烦你了
欢迎光临 批处理之家 (http://bbs.bathome.net/) |
Powered by Discuz! 7.2 |