在Windows命令行中获取接口名称,IP和MAC [英] Get Interface name, IP and MAC in Windows Command line

查看:2162
本文介绍了在Windows命令行中获取接口名称,IP和MAC的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想得到一个机器上的所有接口,IP和MAC地址的列表。我有很多机器从(大约600)获取这些信息,我不能在设备上使用批处理文件。

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.

我需要的所有信息都在 ipconfig / all 但我不知道如何去解析出一个for循环。我还是相当新的一个循环的复杂。基本上我需要得到一个线性输出,如果可能的话。有任何建议吗?

All the info I need is in ipconfig /all but 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?

主机名,接口1名称,IP,Mac,interface2名称,ip mac,... 等。

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

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

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

我的输出不显示 netconnectionID 。还将输出文件作为空行在文本之前。不是一个大的交易,但奇怪。任何建议如何获取所有的信息进入文件正确?这里是我的示例输出。

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.

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


推荐答案

这里是 echo =!HostName!,!NetConID!,!IP_Addrs的 wmic

@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

另一个解决方案解析来自 ipconfig / ALL 的半线性输出并给出最接近上一个 wmic 一个如下:

Another solution parses semi-linear output from ipconfig /ALL and gives results closest to previous wmic one as follows:

@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

这篇关于在Windows命令行中获取接口名称,IP和MAC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆