Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @ECHO off
- SETLOCAL EnableDelayedExpansion
- :: Copyright (C) 2014 demux4555
- :: version 3 (2014.12.15) - does not log out, and uses calc.bat instead of the buggy calc.exe.
- :: Requires Perl, calc.bat, and curl.exe
- :: This script is for retrieving statistical data from the Cisco SG200-08 (SLM2008T) switch, even though it doesn't have SNMP.
- ::
- :: I used Paessler URL Recorder to capture the POST data. You need your own encrypted password (pwd2) to be able to log in.
- :: It's best to use a dedicated user account on your switch for this task, I'd guess. I've named mine "prtg".
- ::
- :: USAGE
- :: curl-switch.bat hostname uname pwd2
- ::
- :: EXAMPLE
- :: curl-switch.bat 192.168.0.51 prtg AAAaBBBbbBCcCCdD
- :: Here you can change the drive and working directory used by this script. Remember to create the directory first!
- C:
- CD \prtg
- :: Here we log in if no previous session found (in a cookie) and snatch the stats.
- :: The switch session timeout has to be longer than the polling interval in PRTG (default 60 seconds).
- IF NOT EXIST ~cookie_%1.txt ( CALL :login %1 %2 %3 )
- curl -s -b ~cookie_%1.txt http://%1/DiagnosticsCPUUtilization.html -o ~DiagnosticsCPUUtilization.html_%1.tmp
- :: If previous page is titled "Moved" we assume there was a failed login, and we login again to create a new cookie.
- FOR /F "tokens=*" %%A in ('findstr /i /c:"<TITLE>Moved" ~DiagnosticsCPUUtilization.html_%1.tmp') DO (
- CALL :login %1 %2 %3
- curl -s -b ~cookie_%1.txt http://%1/DiagnosticsCPUUtilization.html -o ~DiagnosticsCPUUtilization.html_%1.tmp
- )
- 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
- 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
- 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
- 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
- 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
- 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
- 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
- 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
- curl -s -b ~cookie_%1.txt http://%1/SetupSystemSummary.html -o ~SetupSystemSummary.html_%1.tmp
- :: This next curl is to logout. I only use it for troubleshooting.
- 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
- ECHO ^<?xml version="1.0" encoding="Windows-1252" ?^>
- ECHO ^<prtg^>
- :: Resetting various variables
- SET /a switchtemp=0
- SET /a switchtotal=0
- SET /a portnumber=1
- FOR /F "tokens=*" %%G IN ('dir /b ~StatisticsInterface_g?.html_%1.tmp') DO (call :porttraffic "%%G")
- GOTO :Finalize
- :login
- curl -s -c ~cookie_%1.txt -d "uname=%2&pwd2=%3" http://%1/nikola_login.html -o ~nikola_login.html_%1.tmp
- GOTO :eof
- :porttraffic
- :: IMPORTANT: Normally FOR options are enclosed within quotes. If you want to use a quote as part of an option,
- :: then the enclosing quotes must be ditched. That means all characters that the CMD interpreter uses
- :: as token delimiters must be escaped, including space and equal sign. Also the quote needs to be escaped.
- FOR /F tokens^=2^ delims^=^" %%A in ('findstr /i /c:"v_1_3_1" %1') DO (
- SET rx=%%A
- ECHO ^<result^>
- ECHO ^<Channel^>Port g%portnumber% Rx^</Channel^>
- ECHO ^<Value^>%%A^</Value^>
- ECHO ^<Mode^>Difference^</Mode^>
- ECHO ^<Unit^>BytesBandwidth^</Unit^>
- ECHO ^<SpeedSize^>Byte^</SpeedSize^>
- ECHO ^<VolumeSize^>GigaByte^</VolumeSize^>
- ECHO ^<ShowChart^>1^</ShowChart^>
- ECHO ^<ShowTable^>1^</ShowTable^>
- ECHO ^</result^>
- )
- FOR /F tokens^=2^ delims^=^" %%A in ('findstr /i /c:"v_1_12_1" %1') DO (
- SET "tx=%%A"
- ECHO ^<result^>
- ECHO ^<Channel^>Port g%portnumber% Tx^</Channel^>
- ECHO ^<Value^>%%A^</Value^>
- ECHO ^<Mode^>Difference^</Mode^>
- ECHO ^<Unit^>BytesBandwidth^</Unit^>
- ECHO ^<SpeedSize^>Byte^</SpeedSize^>
- ECHO ^<VolumeSize^>GigaByte^</VolumeSize^>
- ECHO ^<ShowChart^>1^</ShowChart^>
- ECHO ^<ShowTable^>1^</ShowTable^>
- ECHO ^</result^>
- )
- SET /a portnumber+=1
- :: Here we calculate/add the total Rx traffic for all ports in the switch. No clue if this is useful info tbh.
- :: It's adding all the received values on all ports.
- :: We have to use an external calculator due to 32 bit integer limitations with SET /A
- FOR /F "tokens=* usebackq" %%A IN (`"calc.bat round0 %switchtemp%+%rx%"`) DO SET switchtotal=%%A
- SET "switchtemp=%switchtotal%"
- GOTO :eof
- :Finalize
- :: This stat is not a very useful stat, because it does not give a good indication of what's been
- :: happening since the last time the script was run. Simply running this script will increase this
- :: value with a couple of percent. I suggest using the 1 minute or 5 minute cpu load number instead.
- REM *** FOR /F tokens^=2^ delims^=^" %%A in ('findstr /i /c:"v_1_4_1" ~DiagnosticsCPUUtilization.html_%1.tmp') DO (
- REM *** ECHO ^<result^>
- REM *** ECHO ^<Channel^>CPU Load 5 sec^</Channel^>
- REM *** ECHO ^<Value^>%%A^</Value^>
- REM *** ECHO ^<Mode^>Absolute^</Mode^>
- REM *** ECHO ^<Unit^>CPU^</Unit^>
- REM *** ECHO ^<Float^>0^</Float^>
- REM *** ECHO ^<ShowChart^>0^</ShowChart^>
- REM *** ECHO ^<ShowTable^>1^</ShowTable^>
- REM *** ECHO ^</result^>
- REM *** )
- FOR /F tokens^=2^ delims^=^" %%A in ('findstr /i /c:"v_1_5_1" ~DiagnosticsCPUUtilization.html_%1.tmp') DO (
- ECHO ^<result^>
- ECHO ^<Channel^>CPU Load 1 min^</Channel^>
- ECHO ^<Value^>%%A^</Value^>
- ECHO ^<Mode^>Absolute^</Mode^>
- ECHO ^<Unit^>CPU^</Unit^>
- ECHO ^<Float^>0^</Float^>
- ECHO ^<ShowChart^>1^</ShowChart^>
- ECHO ^<ShowTable^>1^</ShowTable^>
- ECHO ^</result^>
- )
- FOR /F tokens^=2^ delims^=^" %%A in ('findstr /i /c:"v_1_6_1" ~DiagnosticsCPUUtilization.html_%1.tmp') DO (
- ECHO ^<result^>
- ECHO ^<Channel^>CPU Load 5 min^</Channel^>
- ECHO ^<Value^>%%A^</Value^>
- ECHO ^<Mode^>Absolute^</Mode^>
- ECHO ^<Unit^>CPU^</Unit^>
- ECHO ^<Float^>0^</Float^>
- ECHO ^<ShowChart^>1^</ShowChart^>
- ECHO ^<ShowTable^>1^</ShowTable^>
- ECHO ^</result^>
- )
- :: Small note, my SG 200-08 switch has a total of 18014072 bytes (17592 kB)
- FOR /F tokens^=2^ delims^=^" %%A in ('findstr /i /c:"v_1_9_1" ~DiagnosticsCPUUtilization.html_%1.tmp') DO (
- ECHO ^<result^>
- ECHO ^<Channel^>Free Memory^</Channel^>
- ECHO ^<Value^>%%A^</Value^>
- ECHO ^<Mode^>Absolute^</Mode^>
- ECHO ^<Unit^>BytesMemory^</Unit^>
- ECHO ^<SpeedSize^>Byte^</SpeedSize^>
- ECHO ^<VolumeSize^>Byte^</VolumeSize^>
- ECHO ^<ShowChart^>0^</ShowChart^>
- ECHO ^<ShowTable^>1^</ShowTable^>
- ECHO ^</result^>
- )
- :: Small note, my SG 200-08 switch has a total of 18014072 bytes (17592 kB)
- FOR /F tokens^=2^ delims^=^" %%A in ('findstr /i /c:"v_1_8_1" ~DiagnosticsCPUUtilization.html_%1.tmp') DO (
- ECHO ^<result^>
- ECHO ^<Channel^>Allocated Memory^</Channel^>
- ECHO ^<Value^>%%A^</Value^>
- ECHO ^<Mode^>Absolute^</Mode^>
- ECHO ^<Unit^>BytesMemory^</Unit^>
- ECHO ^<SpeedSize^>Byte^</SpeedSize^>
- ECHO ^<VolumeSize^>Byte^</VolumeSize^>
- ECHO ^<ShowChart^>1^</ShowChart^>
- ECHO ^<ShowTable^>1^</ShowTable^>
- ECHO ^</result^>
- )
- :: Here we calculate uptime in seconds... from human readable format (0 days, 0 hours, 0 minutes 0 seconds)
- FOR /F tokens^=2^ delims^=^" %%a in ('findstr /i /c:"v_1_2_2" ~SetupSystemSummary.html_%1.tmp') DO ( SET "uptime=%%a" )
- FOR /F "tokens=1,3,5,7 " %%a in ("%uptime%") DO ( SET /a "uptimeseconds=(%%a*86400)+(%%b*3600)+(%%c*60)+(%%d)" )
- ECHO ^<result^>
- ECHO ^<Channel^>Uptime^</Channel^>
- ECHO ^<Value^>%uptimeseconds%^</Value^>
- ECHO ^<Mode^>Absolute^</Mode^>
- ECHO ^<Unit^>TimeSeconds^</Unit^>
- ECHO ^<ShowChart^>1^</ShowChart^>
- ECHO ^<ShowTable^>1^</ShowTable^>
- ECHO ^</result^>
- )
- :: Here we output the total received traffic for the switch. No clue if this is useful info tbh.
- :: (There's no point in showing total transmitted, as it will always be the same amount as received.)
- ECHO ^<result^>
- ECHO ^<Channel^>Total Rx^</Channel^>
- ECHO ^<Value^>%switchtotal%^</Value^>
- ECHO ^<Mode^>Difference^</Mode^>
- ECHO ^<Unit^>BytesBandwidth^</Unit^>
- ECHO ^<SpeedSize^>Byte^</SpeedSize^>
- ECHO ^<VolumeSize^>GigaByte^</VolumeSize^>
- ECHO ^<ShowChart^>1^</ShowChart^>
- ECHO ^<ShowTable^>1^</ShowTable^>
- ECHO ^</result^>
- ECHO ^</prtg^>
- :: Clean up and get rid of all the messy tmp files. Not really necessary, but I like to keep a tidy hard drive.
- DEL /Q *_%1.tmp
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement