Get Interface name, IP and MAC in Windows Command line

前端 未结 3 2020
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-13 01:18

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 f

相关标签:
3条回答
  • 2021-01-13 02:05

    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
    
    0 讨论(0)
  • 2021-01-13 02:08

    I would use:

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

    0 讨论(0)
  • 2021-01-13 02:08

    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
    
    0 讨论(0)
提交回复
热议问题