Get Interface name, IP and MAC in Windows Command line

Deadly 提交于 2019-12-01 03:35:56

问题


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.

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?

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

EDIT: 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

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

回答1:


Here is a wmic solution 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):

@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 /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



回答2:


I would use:

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



来源:https://stackoverflow.com/questions/27160042/get-interface-name-ip-and-mac-in-windows-command-line

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!