Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @echo off
- rem Script configuration
- set router_ip=%IP%
- set password_md5=%PW%
- rem Delayed variable expansion in FOR blocks
- setlocal enabledelayedexpansion
- rem Do the login, save the cookie and some json response. Password is sent md5-encoded (your real PW cannot be read from this script).
- echo Login...
- curl --cookie-jar SPW724V.txt --output login.json --stderr nul --data "csrf_token=nulltoken&password=%password_md5%" http://%router_ip%/data/Login.json
- call :get_json_value login.json login login_status
- echo Login status: %login_status%
- if not [%login_status%]==[success] (echo Can not log into router.&pause&exit /b)
- rem Request the index page, to get the CSRF token. This token must be supplied on all subsequent POST requests, or the router will refuse to accept them.
- echo Get index page, extract CSRF token...
- curl --cookie SPW724V.txt --output index.html --stderr nul http://%router_ip%/html/content/overview/index.html
- call :get_csrftoken index.html
- if %csrf%==0 (echo CSRF token not found.&pause&exit /b)
- echo CSRF token: %csrf%
- rem Request the internet connection information.
- curl --cookie SPW724V.txt --output InetIP-before.json --stderr nul http://%router_ip%/data/InetIP.json
- call :get_json_value InetIP-before.json public_ip_v4 wan_ip_before
- echo WAN IP Before: %wan_ip_before%
- if [%wan_ip_before%]==[] (echo Can not get current WAN IP address.)
- rem Disconnect.
- echo Disconnecting...
- curl --cookie SPW724V.txt --output connect-disabled.json --stderr nul --data "req_connect=disabled&csrf_token=%csrf%" http://%router_ip%/data/Connect.json
- call :get_json_value connect-disabled.json onlinestatus onlinestatus
- echo Sent disconnect request.
- rem Sleep 3 seconds. The web site javascript also waits 3000ms.
- sleep 3 >nul
- rem Connect.
- echo Reconnecting...
- curl --cookie SPW724V.txt --output connect-online.json --stderr nul --data "req_connect=online&csrf_token=%csrf%" http://%router_ip%/data/Connect.json
- echo Sent Connect request.
- rem Sleep 3 seconds. The web site javascript also waits 3000ms.
- sleep 3 >nul
- set getipattempt=1
- set maxgetipattempts=80
- echo Probing for new WAN IP address...
- :ipafter
- rem Request the internet connection information again.
- curl --cookie SPW724V.txt --output InetIP-after.json --stderr nul http://%router_ip%/data/InetIP.json
- call :get_json_value InetIP-after.json public_ip_v4 wan_ip_after
- call :get_json_value InetIP-after.json onlinestatus onlinestatus
- if [%wan_ip_after%]==[] (set retrying=Not ready, retrying [%getipattempt%/%maxgetipattempts%]...) else (set retrying=)
- echo Online status: %onlinestatus%, WAN IP: %wan_ip_after%%retrying%
- if [%wan_ip_after%]==[] (
- set /A getipattempt=%getipattempt%+1
- if %getipattempt% LSS %maxgetipattempts% (sleep 2 >nul&goto ipafter)
- )
- rem Logout
- echo Logging out...
- curl --cookie SPW724V.txt --output nul --stderr nul --data "logout=byby&csrf_token=%csrf%" http://%router_ip%/data/Login.json
- set result=SUCCESS
- if [%wan_ip_before%]==[%wan_ip_after%] (set result=FAILURE)
- if [%wan_ip_before%]==[] (set result=UNCLEAR)
- if [%wan_ip_after%]==[] (set result=UNCLEAR)
- echo Deleting temporary files...
- for %%a in (*.json index.html SPW724V.txt) do del %%a
- echo Finished. Result: %result%. New IP: %wan_ip_after%
- exit /b
- rem Extract CSRF token from web site.
- :get_csrftoken
- set csrf=0
- for /F "tokens=1,2,3,4 delims=; " %%a in (%1) do (
- if [%%a%%b%%c]==[varcsrf_token=] (
- set csrf=%%~d
- exit /b
- )
- )
- exit /b
- rem Quick and dirty value reading from the numerous json files the W724V provides.
- rem First arg is json file name, Second is the key name. Third is the name for the return var.
- :get_json_value
- set %3=
- set found_line=0
- for /F "tokens=1,2 delims=:," %%a in (%1) do (
- if !found_line!==1 (
- set %3=%%~b
- exit /b
- )
- if [%%~b]==[%2] (set found_line=1)
- )
- exit /b
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement