Advertisement
unknown14725

curl-switch.bat

Dec 13th, 2014
1,327
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Batch 9.86 KB | None | 0 0
  1. @ECHO off
  2. SETLOCAL EnableDelayedExpansion
  3. :: Copyright (C) 2014 demux4555
  4. :: version 3 (2014.12.15) - does not log out, and uses calc.bat instead of the buggy calc.exe.
  5. :: Requires Perl, calc.bat, and curl.exe
  6. :: This script is for retrieving statistical data from the Cisco SG200-08 (SLM2008T) switch, even though it doesn't have SNMP.
  7. ::
  8. :: I used Paessler URL Recorder to capture the POST data. You need your own encrypted password (pwd2) to be able to log in.
  9. :: It's best to use a dedicated user account on your switch for this task, I'd guess. I've named mine "prtg".
  10. ::
  11. :: USAGE
  12. ::      curl-switch.bat hostname uname pwd2
  13. ::
  14. :: EXAMPLE
  15. ::      curl-switch.bat 192.168.0.51 prtg AAAaBBBbbBCcCCdD
  16.  
  17.  
  18. :: Here you can change the drive and working directory used by this script. Remember to create the directory first!
  19. C:
  20. CD \prtg
  21.  
  22.  
  23. :: Here we log in if no previous session found (in a cookie) and snatch the stats.
  24. :: The switch session timeout has to be longer than the polling interval in PRTG (default 60 seconds).
  25. IF NOT EXIST ~cookie_%1.txt ( CALL :login %1 %2 %3 )
  26.  
  27. curl -s -b ~cookie_%1.txt  http://%1/DiagnosticsCPUUtilization.html -o ~DiagnosticsCPUUtilization.html_%1.tmp
  28. :: If previous page is titled "Moved" we assume there was a failed login, and we login again to create a new cookie.
  29. FOR /F "tokens=*"  %%A in ('findstr /i /c:"<TITLE>Moved" ~DiagnosticsCPUUtilization.html_%1.tmp') DO (
  30.     CALL :login %1 %2 %3
  31.     curl -s -b ~cookie_%1.txt  http://%1/DiagnosticsCPUUtilization.html -o ~DiagnosticsCPUUtilization.html_%1.tmp
  32. )
  33. curl -s -b ~cookie_%1.txt -d "v_1_23_1=g1&submit_target=StatisticsInterface.html" http://%1/StatisticsInterface.html/a1 -o ~StatisticsInterface_g1.html_%1.tmp
  34. curl -s -b ~cookie_%1.txt -d "v_1_23_1=g2&submit_target=StatisticsInterface.html" http://%1/StatisticsInterface.html/a1 -o ~StatisticsInterface_g2.html_%1.tmp
  35. curl -s -b ~cookie_%1.txt -d "v_1_23_1=g3&submit_target=StatisticsInterface.html" http://%1/StatisticsInterface.html/a1 -o ~StatisticsInterface_g3.html_%1.tmp
  36. curl -s -b ~cookie_%1.txt -d "v_1_23_1=g4&submit_target=StatisticsInterface.html" http://%1/StatisticsInterface.html/a1 -o ~StatisticsInterface_g4.html_%1.tmp
  37. curl -s -b ~cookie_%1.txt -d "v_1_23_1=g5&submit_target=StatisticsInterface.html" http://%1/StatisticsInterface.html/a1 -o ~StatisticsInterface_g5.html_%1.tmp
  38. curl -s -b ~cookie_%1.txt -d "v_1_23_1=g6&submit_target=StatisticsInterface.html" http://%1/StatisticsInterface.html/a1 -o ~StatisticsInterface_g6.html_%1.tmp
  39. curl -s -b ~cookie_%1.txt -d "v_1_23_1=g7&submit_target=StatisticsInterface.html" http://%1/StatisticsInterface.html/a1 -o ~StatisticsInterface_g7.html_%1.tmp
  40. curl -s -b ~cookie_%1.txt -d "v_1_23_1=g8&submit_target=StatisticsInterface.html" http://%1/StatisticsInterface.html/a1 -o ~StatisticsInterface_g8.html_%1.tmp
  41. curl -s -b ~cookie_%1.txt  http://%1/SetupSystemSummary.html -o ~SetupSystemSummary.html_%1.tmp
  42.  
  43. :: This next curl is to logout. I only use it for troubleshooting.
  44. REM *** curl -s -b ~cookie_%1.txt -d "language_selector_withAddOption=en-US&err_flag=0&err_msg=&mainpage=nikola_main.html&loginpage=nikola_login.html&submit_flag=8" http://%1/nikola_main.html -o ~nikola_main.html_%1.tmp
  45.  
  46. ECHO ^<?xml version="1.0" encoding="Windows-1252" ?^>
  47. ECHO ^<prtg^>
  48.  
  49. :: Resetting various variables
  50. SET /a switchtemp=0
  51. SET /a switchtotal=0
  52. SET /a portnumber=1
  53.  
  54. FOR /F "tokens=*" %%G IN ('dir /b ~StatisticsInterface_g?.html_%1.tmp') DO (call :porttraffic "%%G")
  55. GOTO :Finalize
  56.  
  57.  
  58. :login
  59.     curl -s -c ~cookie_%1.txt -d "uname=%2&pwd2=%3" http://%1/nikola_login.html -o ~nikola_login.html_%1.tmp
  60. GOTO :eof
  61.  
  62.  
  63. :porttraffic
  64.    :: IMPORTANT: Normally FOR options are enclosed within quotes. If you want to use a quote as part of an option,
  65.    :: then the enclosing quotes must be ditched. That means all characters that the CMD interpreter uses
  66.    :: as token delimiters must be escaped, including space and equal sign. Also the quote needs to be escaped.
  67.     FOR /F tokens^=2^ delims^=^"  %%A in ('findstr /i /c:"v_1_3_1" %1') DO (
  68.         SET rx=%%A
  69.         ECHO    ^<result^>
  70.         ECHO        ^<Channel^>Port g%portnumber% Rx^</Channel^>
  71.         ECHO        ^<Value^>%%A^</Value^>
  72.         ECHO        ^<Mode^>Difference^</Mode^>
  73.         ECHO        ^<Unit^>BytesBandwidth^</Unit^>
  74.         ECHO        ^<SpeedSize^>Byte^</SpeedSize^>
  75.         ECHO        ^<VolumeSize^>GigaByte^</VolumeSize^>
  76.         ECHO        ^<ShowChart^>1^</ShowChart^>
  77.         ECHO        ^<ShowTable^>1^</ShowTable^>
  78.         ECHO    ^</result^>
  79.     )
  80.  
  81.     FOR /F tokens^=2^ delims^=^"  %%A in ('findstr /i /c:"v_1_12_1" %1') DO (
  82.         SET "tx=%%A"
  83.         ECHO    ^<result^>
  84.         ECHO        ^<Channel^>Port g%portnumber% Tx^</Channel^>
  85.         ECHO        ^<Value^>%%A^</Value^>
  86.         ECHO        ^<Mode^>Difference^</Mode^>
  87.         ECHO        ^<Unit^>BytesBandwidth^</Unit^>
  88.         ECHO        ^<SpeedSize^>Byte^</SpeedSize^>
  89.         ECHO        ^<VolumeSize^>GigaByte^</VolumeSize^>
  90.         ECHO        ^<ShowChart^>1^</ShowChart^>
  91.         ECHO        ^<ShowTable^>1^</ShowTable^>
  92.         ECHO    ^</result^>
  93.     )
  94.  
  95.     SET /a portnumber+=1
  96.  
  97.     :: Here we calculate/add the total Rx traffic for all ports in the switch. No clue if this is useful info tbh.
  98.    :: It's adding all the received values on all ports.
  99.    :: We have to use an external calculator due to 32 bit integer limitations with SET /A
  100.     FOR /F "tokens=* usebackq" %%A IN (`"calc.bat round0 %switchtemp%+%rx%"`) DO SET switchtotal=%%A
  101.     SET "switchtemp=%switchtotal%"
  102.  
  103. GOTO :eof
  104.  
  105.  
  106. :Finalize
  107.  
  108. :: This stat is not a very useful stat, because it does not give a good indication of what's been
  109. :: happening since the last time the script was run. Simply running this script will increase this
  110. :: value with a couple of percent. I suggest using the 1 minute or 5 minute cpu load number instead.
  111. REM *** FOR /F tokens^=2^ delims^=^"  %%A in ('findstr /i /c:"v_1_4_1" ~DiagnosticsCPUUtilization.html_%1.tmp') DO (
  112. REM ***     ECHO    ^<result^>
  113. REM ***     ECHO        ^<Channel^>CPU Load 5 sec^</Channel^>
  114. REM ***     ECHO        ^<Value^>%%A^</Value^>
  115. REM ***     ECHO        ^<Mode^>Absolute^</Mode^>
  116. REM ***     ECHO        ^<Unit^>CPU^</Unit^>
  117. REM ***     ECHO        ^<Float^>0^</Float^>
  118. REM ***     ECHO        ^<ShowChart^>0^</ShowChart^>
  119. REM ***     ECHO        ^<ShowTable^>1^</ShowTable^>
  120. REM ***     ECHO    ^</result^>
  121. REM *** )
  122.  
  123. FOR /F tokens^=2^ delims^=^"  %%A in ('findstr /i /c:"v_1_5_1" ~DiagnosticsCPUUtilization.html_%1.tmp') DO (
  124.     ECHO    ^<result^>
  125.     ECHO        ^<Channel^>CPU Load 1 min^</Channel^>
  126.     ECHO        ^<Value^>%%A^</Value^>
  127.     ECHO        ^<Mode^>Absolute^</Mode^>
  128.     ECHO        ^<Unit^>CPU^</Unit^>
  129.     ECHO        ^<Float^>0^</Float^>
  130.     ECHO        ^<ShowChart^>1^</ShowChart^>
  131.     ECHO        ^<ShowTable^>1^</ShowTable^>
  132.     ECHO    ^</result^>
  133. )
  134.  
  135. FOR /F tokens^=2^ delims^=^"  %%A in ('findstr /i /c:"v_1_6_1" ~DiagnosticsCPUUtilization.html_%1.tmp') DO (
  136.     ECHO    ^<result^>
  137.     ECHO        ^<Channel^>CPU Load 5 min^</Channel^>
  138.     ECHO        ^<Value^>%%A^</Value^>
  139.     ECHO        ^<Mode^>Absolute^</Mode^>
  140.     ECHO        ^<Unit^>CPU^</Unit^>
  141.     ECHO        ^<Float^>0^</Float^>
  142.     ECHO        ^<ShowChart^>1^</ShowChart^>
  143.     ECHO        ^<ShowTable^>1^</ShowTable^>
  144.     ECHO    ^</result^>
  145. )
  146.  
  147. :: Small note, my SG 200-08 switch has a total of 18014072 bytes (17592 kB)
  148. FOR /F tokens^=2^ delims^=^"  %%A in ('findstr /i /c:"v_1_9_1" ~DiagnosticsCPUUtilization.html_%1.tmp') DO (
  149.     ECHO    ^<result^>
  150.     ECHO        ^<Channel^>Free Memory^</Channel^>
  151.     ECHO        ^<Value^>%%A^</Value^>
  152.     ECHO        ^<Mode^>Absolute^</Mode^>
  153.     ECHO        ^<Unit^>BytesMemory^</Unit^>
  154.     ECHO        ^<SpeedSize^>Byte^</SpeedSize^>
  155.     ECHO        ^<VolumeSize^>Byte^</VolumeSize^>
  156.     ECHO        ^<ShowChart^>0^</ShowChart^>
  157.     ECHO        ^<ShowTable^>1^</ShowTable^>
  158.     ECHO    ^</result^>
  159. )
  160.  
  161. :: Small note, my SG 200-08 switch has a total of 18014072 bytes  (17592 kB)
  162. FOR /F tokens^=2^ delims^=^"  %%A in ('findstr /i /c:"v_1_8_1" ~DiagnosticsCPUUtilization.html_%1.tmp') DO (
  163.     ECHO    ^<result^>
  164.     ECHO        ^<Channel^>Allocated Memory^</Channel^>
  165.     ECHO        ^<Value^>%%A^</Value^>
  166.     ECHO        ^<Mode^>Absolute^</Mode^>
  167.     ECHO        ^<Unit^>BytesMemory^</Unit^>
  168.     ECHO        ^<SpeedSize^>Byte^</SpeedSize^>
  169.     ECHO        ^<VolumeSize^>Byte^</VolumeSize^>
  170.     ECHO        ^<ShowChart^>1^</ShowChart^>
  171.     ECHO        ^<ShowTable^>1^</ShowTable^>
  172.     ECHO    ^</result^>
  173. )
  174.  
  175. :: Here we calculate uptime in seconds... from human readable format (0 days, 0 hours, 0 minutes 0 seconds)
  176. FOR /F tokens^=2^ delims^=^"  %%a in ('findstr /i /c:"v_1_2_2" ~SetupSystemSummary.html_%1.tmp') DO ( SET "uptime=%%a" )
  177. FOR /F "tokens=1,3,5,7 "  %%a in ("%uptime%") DO ( SET /a "uptimeseconds=(%%a*86400)+(%%b*3600)+(%%c*60)+(%%d)" )
  178. ECHO    ^<result^>
  179. ECHO        ^<Channel^>Uptime^</Channel^>
  180. ECHO        ^<Value^>%uptimeseconds%^</Value^>
  181. ECHO        ^<Mode^>Absolute^</Mode^>
  182. ECHO        ^<Unit^>TimeSeconds^</Unit^>
  183. ECHO        ^<ShowChart^>1^</ShowChart^>
  184. ECHO        ^<ShowTable^>1^</ShowTable^>
  185. ECHO    ^</result^>
  186. )
  187.  
  188. :: Here we output the total received traffic for the switch. No clue if this is useful info tbh.
  189. :: (There's no point in showing total transmitted, as it will always be the same amount as received.)
  190. ECHO    ^<result^>
  191. ECHO        ^<Channel^>Total Rx^</Channel^>
  192. ECHO        ^<Value^>%switchtotal%^</Value^>
  193. ECHO        ^<Mode^>Difference^</Mode^>
  194. ECHO        ^<Unit^>BytesBandwidth^</Unit^>
  195. ECHO        ^<SpeedSize^>Byte^</SpeedSize^>
  196. ECHO        ^<VolumeSize^>GigaByte^</VolumeSize^>
  197. ECHO        ^<ShowChart^>1^</ShowChart^>
  198. ECHO        ^<ShowTable^>1^</ShowTable^>
  199. ECHO    ^</result^>
  200.  
  201. ECHO ^</prtg^>
  202.  
  203. :: Clean up and get rid of all the messy tmp files. Not really necessary, but I like to keep a tidy hard drive.
  204. DEL /Q *_%1.tmp
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement