Guest User

Speedport W724V Reconnect Script v2

a guest
Jul 5th, 2015
2,936
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. @echo off
  2.  
  3. rem Script configuration
  4. set router_ip=%IP%
  5. set password_md5=%PW%
  6.  
  7.  
  8. rem Delayed variable expansion in FOR blocks
  9. setlocal enabledelayedexpansion
  10.  
  11. 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).
  12. echo Login...
  13. curl --cookie-jar SPW724V.txt --output login.json --stderr nul --data "csrf_token=nulltoken&password=%password_md5%" http://%router_ip%/data/Login.json
  14. call :get_json_value login.json login login_status
  15. echo Login status: %login_status%
  16. if not [%login_status%]==[success] (echo Can not log into router.&pause&exit /b)
  17.  
  18. 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.
  19. echo Get index page, extract CSRF token...
  20. curl --cookie SPW724V.txt --output index.html --stderr nul http://%router_ip%/html/content/overview/index.html
  21. call :get_csrftoken index.html
  22. if %csrf%==0 (echo CSRF token not found.&pause&exit /b)
  23. echo CSRF token: %csrf%
  24.  
  25. rem Request the internet connection information.
  26. curl --cookie SPW724V.txt --output InetIP-before.json --stderr nul http://%router_ip%/data/InetIP.json
  27. call :get_json_value InetIP-before.json public_ip_v4 wan_ip_before
  28. echo WAN IP Before: %wan_ip_before%
  29. if [%wan_ip_before%]==[] (echo Can not get current WAN IP address.)
  30.  
  31. rem Disconnect.
  32. echo Disconnecting...
  33. curl --cookie SPW724V.txt --output connect-disabled.json --stderr nul --data "req_connect=disabled&csrf_token=%csrf%" http://%router_ip%/data/Connect.json
  34. call :get_json_value connect-disabled.json onlinestatus onlinestatus
  35. echo Sent disconnect request.
  36.  
  37. rem Sleep 3 seconds.  The web site javascript also waits 3000ms.
  38. sleep 3 >nul
  39.  
  40. rem Connect.
  41. echo Reconnecting...
  42. curl --cookie SPW724V.txt --output connect-online.json --stderr nul --data "req_connect=online&csrf_token=%csrf%" http://%router_ip%/data/Connect.json
  43. echo Sent Connect request.
  44.  
  45. rem Sleep 3 seconds. The web site javascript also waits 3000ms.
  46. sleep 3 >nul
  47.  
  48. set getipattempt=1
  49. set maxgetipattempts=80
  50. echo Probing for new WAN IP address...
  51. :ipafter
  52. rem Request the internet connection information again.
  53. curl --cookie SPW724V.txt --output InetIP-after.json --stderr nul http://%router_ip%/data/InetIP.json
  54. call :get_json_value InetIP-after.json public_ip_v4 wan_ip_after
  55. call :get_json_value InetIP-after.json onlinestatus onlinestatus
  56. if [%wan_ip_after%]==[] (set retrying=Not ready, retrying [%getipattempt%/%maxgetipattempts%]...) else (set retrying=)
  57. echo Online status: %onlinestatus%, WAN IP: %wan_ip_after%%retrying%
  58. if [%wan_ip_after%]==[] (
  59.     set /A getipattempt=%getipattempt%+1
  60.     if %getipattempt% LSS %maxgetipattempts% (sleep 2 >nul&goto ipafter)
  61. )
  62.  
  63. rem Logout
  64. echo Logging out...
  65. curl --cookie SPW724V.txt --output nul --stderr nul --data "logout=byby&csrf_token=%csrf%" http://%router_ip%/data/Login.json
  66.  
  67. set result=SUCCESS
  68. if [%wan_ip_before%]==[%wan_ip_after%] (set result=FAILURE)
  69. if [%wan_ip_before%]==[] (set result=UNCLEAR)
  70. if [%wan_ip_after%]==[] (set result=UNCLEAR)
  71.  
  72. echo Deleting temporary files...
  73. for %%a in (*.json index.html SPW724V.txt) do del %%a
  74.  
  75. echo Finished. Result: %result%. New IP: %wan_ip_after%
  76.  
  77. exit /b
  78.  
  79. rem Extract CSRF token from web site.
  80. :get_csrftoken
  81. set csrf=0
  82. for /F "tokens=1,2,3,4 delims=; " %%a in (%1) do (
  83.     if [%%a%%b%%c]==[varcsrf_token=] (
  84.         set csrf=%%~d
  85.         exit /b
  86.     )
  87. )
  88. exit /b
  89.  
  90. rem Quick and dirty value reading from the numerous json files the W724V provides.
  91. rem First arg is json file name, Second is the key name. Third is the name for the return var.
  92. :get_json_value
  93. set %3=
  94. set found_line=0
  95. for /F "tokens=1,2 delims=:," %%a in (%1) do (
  96.     if !found_line!==1 (
  97.         set %3=%%~b
  98.         exit /b
  99.     )
  100.     if [%%~b]==[%2] (set found_line=1)
  101. )
  102. exit /b
RAW Paste Data