5

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
C.Champagne
  • 5,381
  • 2
  • 23
  • 35
scott
  • 143
  • 1
  • 2
  • 12
  • Start with `for /F "tokens=1* delims=:" %G in ('ipconfig /all') do @echo %G %H` to see an example. [Learn more ...](http://ss64.com/nt/for_cmd.html). Then you could replace that too simple `@echo %G %H` with [multiple commands in your FOR loop](http://ss64.com/nt/for.html). Feel free to [update your question](http://stackoverflow.com/posts/27160042/edit) with actual achievements and ask more in case you crane at something. – JosefZ Nov 29 '14 at 16:40
  • Look into `wmic nicconfig /?` Then do something like this: `wmic /node:remotePCnetname_or_IP /user:domain\admin /password:domainAdminPass nicconfig where 'DNSDomain like "%.%" get MACAddress, IPAddress, DNSHostName /format:csv` (but salt to taste). This is not the ultimate answer, but this is your path to finding your own answer. *Give a man a fish and he'll eat for a day; but teach him to use the Internet, and he won't bother you for weeks. --Steven Wright* – rojo Dec 01 '14 at 13:42
  • 1
    My issue with a for loop on the ipconfig /all was how to get different sets of data for each line. the wmic is closer but I am not sure how to combine two queries. 'wmic nic get netconnectionID' this will get me the interface names but this gets me the IP and MACs that I need and only shows me the enabled interfaces so it gets rid of the server 2008 tunnel junk. 'wmic nicconfig where IPEnabled=True get ipaddress, macaddress' If I can get these combined it gets me a lot closer to an output I can toss into excel and drop a macro on. – scott Dec 03 '14 at 21:39
  • 1
    Another update. So these three commands are what I need to figure out how to put together. I would not mind if the hostname was on each line. wmic computersystem get name wmic nic where netenabled=true get netconnectionID wmic nicconfig where IPEnabled=True get ipaddress, macaddress – scott Dec 03 '14 at 22:01

3 Answers3

5

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
JosefZ
  • 28,460
  • 5
  • 44
  • 83
4

I would use:

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

jacetyh
  • 41
  • 4
1

asked: I can't use a batch file on the devices. I would like to send the command and get back an echoed output.
my solution try to use a single commands row.
Also I use 2 loops to better refine the data search.

so 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
LelioS
  • 11
  • 2
  • 2
    Please add more details about your solution, why does your solution works? what was the user that asked the question doing wrong? Please do this in order to complete your answer, in this way it will help the people with the same question in the future. Thanks! – EnriqueBet May 25 '20 at 20:25