wmic 扫描 windows 然后执行其余命令?

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"
)

好的,我的脚本快完成了!我需要它来扫描 windows 台计算机,但我不知道如何扫描。

我可以使用 wmic os get name 并查找 Windows 吗?

喜欢 wmic os get name | find "Windows" do () 不会工作但是...

是这样的吗?

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"
    )
  )
)

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.

获取 TTL 变量 _ready 的值:它是 TTL= 行中的第 9 个标记 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"
    )
  )
)

仅供参考,我的(较旧的)批处理脚本 returns OS 正在路由到远程计算机。为了您的目的,请注意 rem echo debug G: %%~G "%%~H" "%%~I"echo(!no:~-3! !ip:~0,15! !tl:~-3! !oss! !hna! 之间的代码片段。注意不能区分硬件(路由器)和*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

输出(为保护隐私删除了跃点 1..4):

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

==>