Advertisement
Moktart

Cmdshell Monster

Dec 27th, 2012
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. :This script polls a list of computers and uses WMI to extract the products table.
  2. :If a target is successfully pinged it requests the computer name from WMI and
  3. :fails if the target name given doesn't match WMI.  It extracts the products table
  4. :into a text file named after the target with no extension, putting the target name
  5. :at the top of the file.  This data can later be searched with FINDSTR or imported
  6. :into a SQL table with a separate script.  Using the "repeat" variable, this script
  7. :can be configured to repeat ad infinitum.  It will not re-poll targets that have
  8. :had their products table successfully extracted unless the "refresh" bit is set.
  9. :It also logs WMI errors, although the format of the output file needs work to be useful.
  10.  
  11.  
  12. :-------------------------------------------------------
  13. :This section contains editable variables.
  14. :-------------------------------------------------------
  15. :Turning this flag on (1) will make the script loop indefinitely using the number
  16. :of threads specified in %threads%.
  17. set repeat=0
  18. set threads=30
  19.  
  20. :Setting this flag will poll all machines in the list whether or not they
  21. :have been successfully polled in the past.
  22. set refresh=0
  23.  
  24. :This is the list of targets.  It should have a column header or a
  25. :blank line at the top.
  26. set sourcelist=computerlist.txt
  27.  
  28. :Uncomment this line to generate and use a list of all computer objects (including disabled accounts) in the domain.
  29. REM dsquery * "dc=webtrends,dc=corp" -filter "(objectclass=computer)" -attr cn -l -limit 0 > computerlist.txt
  30.  
  31.  
  32. :-------------------------------------------------------
  33.  
  34.  
  35.  
  36. :start
  37.  
  38. @echo off
  39. setlocal enabledelayedexpansion
  40. cls
  41.  
  42. if not exist .\output mkdir output
  43.  
  44. if "%1" GTR "" goto :begin
  45. if exist results.txt del results.txt
  46. if exist inst* del inst*
  47.  
  48. set instance=INST%random%
  49. set scriptname=WMIScrapeMultix.bat
  50.  
  51. set counter=-1
  52. for /f "tokens=*" %%i in (%sourcelist%) do set /a counter +=1
  53.  
  54.  
  55.  
  56. if %repeat% equ 0 (
  57.     Echo The file has %counter% entries.  How many threads?
  58.     set /p threads=Threads:
  59.     )
  60.    
  61.  
  62. set /a batch=%counter% / %threads%
  63. set modulus=%counter%
  64. set /a modulus %%=%batch%
  65. set threadcount=0
  66. set OS=0
  67.  
  68.  
  69. :ThreadParse
  70. set counter=0
  71. set skip=0
  72. set /a skip =%threadcount% * %batch%
  73. set /a threadcount += 1
  74. if %skip% equ 0 set skip=1
  75.  
  76. if %threadcount% EQU %threads% set /a batch +=%modulus%
  77.  
  78. for /f "tokens=* skip=%skip%" %%i in (%sourcelist%) do (
  79.     if !counter! LEQ !batch! (
  80.         echo %%i >> %instance%.%threadcount%
  81.         set /a counter +=1
  82.         )
  83.     )
  84.  
  85. if %threadcount% LSS %threads% goto :threadparse
  86.  
  87. for /L %%i in (1,1,%threads%) do start /min %scriptname% %instance% %%i
  88.  
  89. call :waiting
  90.  
  91. for /f "usebackq" %%i in (`dir /b *.results`) do (
  92.     type %%i >> results.txt
  93.     del %%i
  94.     )
  95. for /f "usebackq" %%i in (`dir /b *.WMIErrors`) do (
  96.     type %%i >> WMIErrors.txt
  97.     del %%i
  98.     )
  99.  
  100. if %repeat% EQU 1 (
  101.     cls
  102.     Echo If you want to stop this nonsense, hit CTRL-C now.  You have five seconds.
  103.     >nul ping -n 1 -w 6000 1.0.0.1
  104.     goto :start
  105.     )
  106.    
  107. goto :eof
  108.  
  109.  
  110.  
  111.  
  112.  
  113.  
  114. :waiting
  115. cls
  116. echo Processing.
  117. >nul ping -n 1 -w 1000 1.0.0.1
  118. cls
  119. echo Processing..
  120. >nul ping -n 1 -w 1000 1.0.0.1
  121. cls
  122. echo Processing...
  123. >nul ping -n 1 -w 1000 1.0.0.1
  124. cls
  125. echo Processing....
  126. >nul ping -n 1 -w 1000 1.0.0.1
  127.  
  128.  
  129. if exist %instance%.? goto :waiting
  130. if exist %instance%.?? goto :waiting
  131. goto :eof
  132.  
  133.  
  134.  
  135. :begin
  136. @echo off
  137. cls
  138.  
  139. setlocal enabledelayedexpansion
  140.  
  141. set thread=%2
  142. set infile=%1
  143. set myfile=%infile%.%thread%
  144. set mytemp=%infile%.%thread%.tmp
  145. set myname=%infile%.%thread%.name
  146. set myresults=%infile%.%thread%.results
  147.  
  148.  
  149. set size=0
  150. set counter=0
  151. set total=0
  152. for /f "tokens=*" %%i in (%myfile%) do set /a total +=1
  153.  
  154. for /f "tokens=1" %%i in (%myfile%) do (
  155.     set /a counter +=1
  156.     echo %%i - !counter! of %total%
  157.     set target=%%i
  158.     set target=!target: =!
  159.     if %refresh% equ 1 (
  160.        
  161.         ping %%i -n 4 -w 1000 > %mytemp%
  162.         > nul findstr "Reply from" %mytemp%
  163.         if !errorlevel! EQU 0 (
  164.             call :work %%i
  165.             ) ELSE (
  166.             echo FailedPing,None,%%i >> %myresults%
  167.             )
  168.         ) ELSE (
  169.         :echo NotRefreshing!
  170.             If not exist .\output\!target! (
  171.                 ping %%i -n 4 -w 1000 > %mytemp%
  172.                 > nul findstr "Reply from" %mytemp%
  173.                 if !errorlevel! EQU 0 (
  174.                     call :work %%i
  175.                     ) ELSE (
  176.                     echo FailedPing,None,%%i >> %myresults%
  177.                 )
  178.             )
  179.         )
  180.     )
  181.    
  182.  
  183. )
  184.  
  185.     if exist %mytemp% del %mytemp%
  186.     if exist %myname% del %myname%
  187.     if exist %myname%.txt del %myname%.txt
  188.     if exist %myfile% del %myfile%
  189.     exit
  190.    
  191. goto :eof
  192.  
  193.  
  194. :work
  195.  
  196.     set size=0
  197.     set target=%1
  198.     set target=%target: =%
  199.     set name=0
  200.     set os=0
  201.     if exist %mytemp% del %mytemp%
  202.     if exist %myname% del %myname%
  203.     if exist %myname%.txt del %myname%.txt
  204.         call :nameget
  205.         call :osget
  206.  
  207.        
  208.         if !name! LEQ 2 (
  209.             echo FailedWMIName,None,%target% >> %myresults%
  210.             goto :eof
  211.             )
  212.         if /I "!name!" NEQ "%target%" (
  213.             echo DNSMismatch,"!Name!",%Target%,!os! >> %myresults%
  214.             goto :eof
  215.         )
  216.        
  217.        
  218.         echo !name! > %myname%
  219.         REM wmic /node:"%target%" systemenclosure get serialnumber >> %mytemp% 2>>%myfile%.WMIErrors
  220.         wmic /node:"%target%" product get name >> %mytemp% 2>>%myfile%.WMIErrors
  221.         call :filecheck %mytemp%
  222.         if !size! GTR 6 (
  223.             type %mytemp% >> %myname%
  224.             >nul copy %myname% .\output\!name!
  225.             echo Success,!name!,%target%,!os! >> %myresults%
  226.             ) else (
  227.             echo FailedWMIProducts,!name!,%target%,!os! >> %myresults%
  228.         )
  229.        
  230.    
  231.  
  232. goto :eof
  233.  
  234. :nameget
  235. 2>>%myfile%.WMIErrors wmic /node:"%target%" /failfast:on ComputerSystem get name  |findstr /v "Name" > %myname%.txt
  236.         for /f "tokens=1" %%i in (%myname%.txt) do (
  237.             set name=%%i
  238.             set name=!name: =!
  239.             del %myname%.txt
  240.             exit /b 9993
  241.             )
  242. goto :eof
  243.  
  244. :osget
  245. 2>>%myfile%.WMIErrors wmic /node:"%target%" /failfast:on OS get name  |findstr /v "Name" > %myname%.txt
  246.         for /f "tokens=*" %%i in (%myname%.txt) do (
  247.             set OS=%%i
  248.             del %myname%.txt
  249.             exit /b 9993
  250.             )
  251. goto :eof
  252.  
  253.  
  254. :filecheck
  255. set size=%~z1
  256. goto :eof
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement