在 Windows 命令行中获取接口名称、IP 和 MAC

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27160042/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 18:51:08  来源:igfitidea点击:

Get Interface name, IP and MAC in Windows Command line

windowscommand-linewmicipconfig

提问by scott

I want to get a list of all the interfaces, IP and MAC address on a machine. I have quite a few machines to get this information from (around 600) and I can't use a batch file on the devices. I would like to send the command and get back an echoed output.

我想获取机器上所有接口、IP 和 MAC 地址的列表。我有很多机器可以从(大约 600 台)获取此信息,但我无法在这些设备上使用批处理文件。我想发送命令并返回回显输出。

All the info I need is in ipconfig /allbut I am not sure how to go about parsing that out with a for loop. I am still quite new to that complex of a loop. Essentially I need to get a one liner output if possible. Any suggestions?

我需要的所有信息都在里面,ipconfig /all但我不知道如何用 for 循环解析它。我对复杂的循环仍然很陌生。如果可能的话,基本上我需要得到一个单行输出。有什么建议?

hostname, interface1 name, IP, Mac, interface2 name, ip mac,...etc.

hostname, interface1 name, IP, Mac, interface2 name, ip mac,...等等。

EDIT: I have had some luck with the WMICoutputs but I am having issues getting it to display correctly in a file. If I run these.

编辑:我对WMIC输出有一些运气,但我在让它在文件中正确显示时遇到问题。如果我运行这些。

wmic computersystem get name  
wmic nic where netenabled=true get netconnectionID   
wmic /output:C:\wmictest.csv nicconfig where IPEnabled=True get ipaddress, macaddress /format:csv

My output does not show the netconnectionID. Also the output file as a blank line before the text. not a big deal but is odd. Any suggestions on how to get all the info into the file correctly? here is my example output.

我的输出没有显示netconnectionID. 还将输出文件作为文本前的空行。没什么大不了的,但很奇怪。关于如何将所有信息正确导入文件的任何建议?这是我的示例输出。

Node,IPAddress,MACAddress  
U8001674-TPL-A,{10.91.35.84;fe80::52b:9817:6bbf:dca4},F0:1F:AF:2A:5E:B5

回答by JosefZ

Here is a wmicsolution resulting with echo =!HostName!,!NetConID!,!IP_Addrs!,!MAC_Addr!with one line for each active adapter of a local computer (can't try adaptations necessary to run it against a remote machine):

这是一个wmic解决方案echo =!HostName!,!NetConID!,!IP_Addrs!,!MAC_Addr!,本地计算机的每个活动适配器都有一行(无法尝试针对远程机器运行它所需的调整):

@ECHO OFF >NUL
@SETLOCAL enableextensions enabledelayedexpansion
set "HostName="
wmic computersystem get name ^
  /format:textvaluelist.xsl>"%temp%\cmptr.txt" 2>nul
for /F "tokens=1* delims==" %%G in ('type "%temp%\cmptr.txt"') do (
  if /i "%%G"=="Name" set "HostName=%%~H"
)
set "MAC_Addr="
for /F "tokens=1* delims=:" %%G in ('ipconfig /all^|find /i "Physical Address"') do (
  set "foo="
  for %%I in (%%~H) do if not "%%~I"=="" set "foo=%%~I"
  set "MAC_Addr=!foo:-=:!"
  set "NetConID="
  wmic nic where "NetEnabled='true' and MACAddress='!MAC_Addr!'" ^
    list /format:textvaluelist.xsl>"%temp%\wmcnc.txt" 2>&1
  for /F "tokens=1* delims==" %%I in ('type "%temp%\wmcnc.txt"') do (
    if /i "%%I"=="NetConnectionID" set "NetConID=%%~J"
  )
  set "IP_Addrs="
  wmic nicconfig where "IPEnabled='True' and MACAddress='!MAC_Addr!'" ^
    list /format:textvaluelist.xsl>"%temp%\wmcnccfg.txt" 2>&1
  for /F "tokens=1* delims==" %%I in ('type "%temp%\wmcnccfg.txt"') do (
    if /i "%%I"=="IPAddress" set "IP_Addrs=%%~J"
  )
  if not "!NetConID!,!IP_Addrs!"=="," (
    @echo =!HostName!,!NetConID!,!IP_Addrs!,!MAC_Addr!
  )
)
:endlocal
del "%temp%\cmptr.txt" 2>nul
del "%temp%\wmcnc.txt" 2>nul
del "%temp%\wmcnccfg.txt" 2>nul
@ENDLOCAL
goto :eof

Another solution parses semi-linear output from ipconfig /ALLand gives results closest to previous wmicone as follows:

另一种解决方案解析半线性输出ipconfig /ALL并给出与前wmic一个最接近的结果,如下所示:

@ECHO OFF >NUL
@SETLOCAL enableextensions enabledelayedexpansion
  set "HostName="
  set "NetConID="
  set "IP_Addr4="
  set "IP_Addr6="
  set "MAC_Addr="
for /F "tokens=1* delims=:" %%G in ('ipconfig /ALL') do (
  set "foo=%%~G"
  if not "!foo:Host name=!"=="!foo!" (
    for %%I in (%%~H) do if not "%%~I"=="" set "HostName=%%~I"
  )
  if "!foo:adapter=!"=="!foo!" (
    if not "!foo:Physical Address=!"=="!foo!" (
      for %%I in (%%~H) do if not "%%~I"=="" set "MAC_Addr=%%~I"
    )
    if not "!foo:IPv4 Address=!"=="!foo!" (
      for %%I in (%%~H) do if not "%%~I"=="" set "IP_Addr4=%%~I"
      set "IP_Addr4=!IP_Addr4:(preferred)=!"
    )
    if not "!foo:local IPv6 Address=!"=="!foo!" (
      for %%I in (%%~H) do (
        if not "%%~I"=="" (
          for /F "delims=%%" %%p in ("%%~I") Do set "IP_Addr6=%%~p"
          rem set "IP_Addr6=!IP_Addr6:(preferred)=!"
        )
      )
    )
  ) else (
    if not "!IP_Addr6!,!IP_Addr4!"=="," (
      @echo #!HostName!,!NetConID!,{"!IP_Addr4!","!IP_Addr6!"},!MAC_Addr!
    )
    set "MAC_Addr="
    set "IP_Addr4="
    set "IP_Addr6="
    set "NetConID=!foo:*adapter =!"
  )
)
if not "!IP_Addr6!,!IP_Addr4!"=="," (
  @echo =!HostName!,!NetConID!,{"!IP_Addr4!","!IP_Addr6!"},!MAC_Addr!
)

:endlocal
@ENDLOCAL
goto :eof

回答by user3337126

I would use:

我会用:

wmic nicconfig where "IPEnabled = True" get ipaddress... and things you would like to get. There's no need to parse output.

wmic nicconfig where "IPEnabled = True" get ipaddress......以及你想得到的东西。无需解析输出。

回答by LelioS

try this solution too:

也试试这个解决方案:

@for /f "skip=2 delims=, tokens=1,2,3,4" %L in ('wmic nic where "netenabled=true" get macaddress^,index^,netconnectionid^,productname /format:csv') do @for /f "skip=2 delims={}, tokens=2" %A in ('wmic nicconfig where "index=%M" get ipaddress^,ipsubnet ^/format:csv') do @echo %L - %O - %N - %A