Board logo

标题: [文本处理] [以解决]批处理如何提取多个网络连接的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 接口的状态信息.

是从一个更长的代码改写的, 实测可以获得你要的输出, 当然代码中还有冗余的部分, 我不想继续改了, 楼主有兴趣就自行给代码瘦身吧
  1. @echo off & setlocal enabledelayedexpansion
  2. echo 扫描 NIC 接口...
  3. (call :getConnectionName nicCnt) & cls
  4. if !nicCnt! leq 0 ( echo 没找到在运行的 NIC 接口 & exit )
  5. call :viewNIC
  6. pause
  7. exit
  8. :viewNIC
  9. >NIC.TXT (
  10.   for /f "tokens=1,2 delims==" %%a in ('wmic nicconfig where "ipenabled='true'" get /value') do (
  11.     if "%%a" gtr "" set "%%a=%%b"
  12.     if /i "%%a"=="Index" (
  13.       set /a "%%a=%%b"
  14.       for /f "tokens=1,2 delims==" %%x in ('wmic nic where "index=!index!" get /value') do (
  15.         if "%%x" gtr "" set "%%x=%%y"
  16.         if /i "%%x"=="NetConnectionStatus" for %%z in (%%y) do set "%%x=!%%x%%z!"
  17.         if /i "%%x"=="ConfigManagerErrorCode" for %%z in (%%y) do set "%%x=!%%x%%z!"
  18.       )
  19.     )
  20.     if /i "%%a"=="WINSSecondaryServer" (
  21.       call :getIP IPAddress& call :getIP IPSubnet& call :getIP DefaultIPGateway& call :getIP DNSServerSearchOrder
  22.       for %%n in (Description NetConnectionID IPAddress) do (
  23.         for /f "delims=" %%a in ("!%%n!") do set "%%n=%%~a"
  24.       )
  25.       
  26.       REM 去掉 ipv6 的 ip 地址
  27.       for /f "delims=," %%a in ("!IPAddress!") do set "IPAddress=%%a"
  28.       for %%n in ("!NetConnectionID!") do (
  29.         if not "%%~n" gtr "" (
  30.           echo;!Description!: !IPAddress!
  31.         ) else (
  32.           echo;!NetConnectionID!: !IPAddress!
  33.         )
  34.       )
  35.     )
  36.   )
  37. )
  38. start NIC.TXT
  39. exit /b
  40. :getIP
  41. (set %1=!%1:^"=!&set %1=!%1:{=!&set %1=!%1:}=!)
  42. exit /b
  43. :getEnum
  44. for /f "tokens=1,2* delims= " %%a in (%1) do (
  45.   if /i "!getValue!"=="Y" set "%2%%a=%%c"
  46.   if /i "%%a"==":%2" set getValue=Y
  47.   if /i "%%a"==":end:%2" set getValue=N
  48. )
  49. exit /b
  50. :getConnectionName nicCnt
  51. set /a %1=0
  52. (
  53.   echo   IP enabled NIC:
  54.   for /f "tokens=1,2 delims==" %%a in ('wmic nicconfig where "ipenabled='true'" get Index^,MACAddress /value') do (
  55.     set MACAddress=
  56.     for /f "delims=" %%u in ("%%a") do for /f "delims=" %%v in ("%%b") do (
  57.       if "%%u" neq "" set "%%u=%%v"
  58.     )
  59.     if /i "%%a"=="Index" (
  60.       set NetConnectionID=
  61.       for /f "tokens=1,2 delims==" %%x in ('wmic nic where "index=!index!" get NetConnectionID^,Description /value') do (
  62.         for /f "delims=" %%u in ("%%x") do for /f "delims=" %%v in ("%%y") do (
  63.           if "%%u" neq "" set "%%u=%%v"
  64.         )
  65.       )
  66.     ) else if /i "%%a"=="MACAddress" (
  67.       set /a %1+=1
  68.       set /a "NIC_Index!%1!=index"
  69.       if "!NetConnectionID!"=="" (set "NCID!%1!=!Description!"
  70.       ) else set "NCID!%1!=!NetConnectionID!"
  71.       set "MACAddress!%1!=!MACAddress!"
  72.       for %%k in (!%1!) do (
  73.         echo !%1!. !NCID%%k!
  74.         echo    MAC: !MACAddress%%k!
  75.       )
  76.     )
  77.   )
  78. )
  79. 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