Search code examples
findwmic

wmic scan for windows then do rest of command?


    for /f "tokens=*" %%a in (ip.txt) do (
  set "_ready="
  for /F %%G in ('ping -4 -n 1 %%a^|find "TTL="') do set "_ready=%%G"
  if defined _ready (
      rem your `WMIC /FAILFAST:OFF /node:%%a …` 
         wmic /node:%%a /user: /password: computersystem get Name, domain, Manufacturer, Model, NumberofProcessors, PrimaryOwnerName,Username, Roles, totalphysicalmemory /format:list
         wmic /node:%%a /user: /password: cpu get Name, Caption, MaxClockSpeed, DeviceID, status /format:list
         wmic /node:%%a /user: /password: path Win32_VideoController get Name, status, DeviceID /format:list
         wmic /node:%%a /user: /password: os get Version, Caption, CountryCode, CSName, Description, InstallDate, SerialNumber, ServicePackMajorVersion, WindowsDirectory /format:list
         wmic /node:%%a /user: /password: csproduct get identifyingnumber /format:list

  )>"%%a.txt"
)

ok so my script is almost done! i need it to scan for windows computers and i dont know how.

could i use the wmic os get name and look for Windows ?

like wmic os get name | find "Windows" do () wont work but...

so something like this ?

set "_ready="
for /F "tokens=9 delims=<=> " %%G in ('
    ping -4 -n 1 "%%~a" ^| find /I "TTL="') do (
  set "_ready=%%G"
)
set "oss=unknown"
if defined _ready (
  if !_ready! LEQ 64 (
      set "oss=*nix/HW"
  ) else ( 
    if !_ready! LEQ 128 (
      set "oss=Windows"
      rem your `WMIC` for /f "tokens=*" %%a in (ip.txt) do (
  set "_ready="
  for /F %%G in ('ping -4 -n 1 %%a^|find "TTL="') do set "_ready=%%G"
  if defined _ready (
      rem your `WMIC /FAILFAST:OFF /node:%%a …` 
         wmic /node:%%a /user: /password: computersystem get Name, domain, Manufacturer, Model, NumberofProcessors, PrimaryOwnerName,Username, Roles, totalphysicalmemory /format:list
         wmic /node:%%a /user: /password: cpu get Name, Caption, MaxClockSpeed, DeviceID, status /format:list
         wmic /node:%%a /user: /password: path Win32_VideoController get Name, status, DeviceID /format:list
         wmic /node:%%a /user: /password: os get Version, Caption, CountryCode, CSName, Description, InstallDate, SerialNumber, ServicePackMajorVersion, WindowsDirectory /format:list
         wmic /node:%%a /user: /password: csproduct get identifyingnumber /format:list

  )>"%%a.txt"
)

    ) else (
      set "oss=Solaris"
    )
  )
)

Solution

  • How to identify which OS is running at remote host?

    You can use nmap. It isn't precise, but it can give you a clue. Or you can use a simple "ping" and look for the TTL.

    • TTL=64 = *nix - the hop count so if your getting 61 then there are 3 hops and its a *nix device. Most likely Linux.
    • TTL=128 = Windows - again if the TTL is 127 then the hop is 1 and its a Windows box.
    • TTL=254 = Solaris/AIX - again if the TTL is 250 then the hop count is 4 and its a Solaris box.

    Get TTL value to variable _ready: it's the 9th token in TTL= line of succesful ping:

    set "_ready="
    for /F "tokens=9 delims=<=> " %%G in ('
        ping -4 -n 1 "%%~a" ^| find /I "TTL="') do (
      set "_ready=%%G"
    )
    set "oss=unknown"
    if defined _ready (
      if !_ready! LEQ 64 (
          set "oss=*nix/HW"
      ) else ( 
        if !_ready! LEQ 128 (
          set "oss=Windows"
          rem your `WMIC` commands here 
        ) else (
          set "oss=Solaris"
        )
      )
    )
    

    FYI, my (older) batch scripts returns OSes on route to a remote computer. For your purposes, pay your attention to code snippet between rem echo debug G: %%~G "%%~H" "%%~I" and echo(!no:~-3! !ip:~0,15! !tl:~-3! !oss! !hna!. Note that it can't distinguish between hardware (router) and *nix OS:

    @ECHO OFF >NUL
    SETLOCAL EnableExtensions EnableDelayedExpansion
    set "cmnd=tracert -d -4"
    if "%~1"=="" (
        set "target=%COMPUTERNAME%" 
    ) else (
        if "%~1"=="-1" ( 
            set "target=d:\bat\files\tracertgol.txt"
            set "cmnd=type"
        ) else (
            set "target=%~1"
        )
    )
    for %%a in ("%target%") do (
      echo checking %%a
      for /F "tokens=1,2,8 delims= " %%G in ('
        %cmnd% "%%~a" ^| findstr /R /B "..[0-9].*[1-9][0-9]*\.[1-9][0-9]*\.[1-9][0-9]*\.[1-9][0-9]*" 
        ') do (
        rem echo debug  G: %%~G "%%~H" "%%~I"
        set "ttl="
        set "hna="
        for /F "tokens=1,2,9 delims=<=> " %%x in ('
            ping -a -4 -n 1 "%%~I" ^| findstr /I "Pinging TTL"') do (
          rem echo debug x: %%~G %%~H "%%~I" %%x [%%y] %%z
          if /I "%%~x"=="Pinging" ( set "hna=%%~y" ) else ( set "ttl=%%z" )
        )
        set "oss=unknown"
        if defined ttl (
          if !ttl! LEQ 64 (
              set "oss=*nix/HW"
          ) else ( 
            if !ttl! LEQ 128 (
              set "oss=Windows"
            ) else (
              set "oss=Solaris"
            )
          )
        )
        rem call :display %%G %%I "!ttl!"   "!oss!" "!hna!"
        set "no=   %%~G"
        set "ip=%%~I               "
        set "tl=   !ttl!"
        echo(!no:~-3! !ip:~0,15! !tl:~-3! !oss! !hna!
      )
    )
    :endlocal
    ENDLOCAL
    goto :eof
    
    :display
    SETLOCAL
      set "no=   %~1"
      set "ip=%~2               "
      set "tl=   %~3"
      echo(%no:~-3% %ip:~0,15% %tl:~-3% %~4 %~5
    ENDLOCAL
    goto :eof
    

    Output (hops 1..4 removed for privacy):

    d:\bat> ping -4 -n 1 volny.cz|find "TTL="
    Reply from 46.255.231.48: bytes=32 time=9ms TTL=248
    
    ==> D:\bat\SO\TTL.bat volny.cz
    checking "volny.cz"
      5 213.29.165.78   249 Solaris ph700-ex1-be2.cz.net
      6 91.210.16.113   249 Solaris nix4.centrum.cz
      7 46.255.229.38   249 Solaris v4000.c01.stl.net.chservices.cz
      8 46.255.231.48   248 Solaris bbx-fe-hp-pool.centrum.cz
    
    ==> ping -4 -n 1 google.cz|find "TTL="
    Reply from 173.194.112.111: bytes=32 time=17ms TTL=56
    
    ==> D:\bat\SO\TTL.bat google.cz
    checking "google.cz"
      5 193.85.195.94    60 *nix/HW ae-2.fra2027-ex1.gtsce.net
      6 74.125.49.1      59 *nix/HW 74.125.49.1
      7 216.239.56.114   58 *nix/HW 216.239.56.114
      8 72.14.236.55        unknown 72.14.236.55
      9 173.194.112.111  56 *nix/HW fra07s30-in-f15.1e100.net
    
    ==>