Skip to content

Instantly share code, notes, and snippets.

@arianrashidi
Last active February 11, 2021 10:55
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save arianrashidi/85739514ffabfb50544dc96b5b6f9da2 to your computer and use it in GitHub Desktop.
Save arianrashidi/85739514ffabfb50544dc96b5b6f9da2 to your computer and use it in GitHub Desktop.
Router Reconnect
#!/usr/bin/env bash
############################################################################
# "Telekom Speedport W 724V Type B" Reconnect
############################################################################
# 2016-04-18 Arian Rashidi
############################################################################
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
########################################################################
ROUTER_IP="192.168.2.1"
ROUTER_PASSWORD_MD5=""
PATH_TMP_FILE="$HOME/speedport.tmp"
PATH_COOKIE="$HOME/speedport.cookie.tmp"
echo "Old IP: $(curl -s my-ip-is.appspot.com/plain)"
echo "Extracting token from front page..."
curl -s --output $PATH_TMP_FILE "http://${ROUTER_IP}/html/login/index.html?lang=en" > /dev/null
TOKEN_LOGIN=$(grep -oh '_httoken = [0-9]\{5,15\};' $PATH_TMP_FILE | tr -dc '0-9')
if ! [[ "$TOKEN_LOGIN" =~ ^[0-9]+$ ]]; then echo 'Error: Extracting the token failed.' && exit 0; fi
echo "Login..."
curl -s --cookie-jar $PATH_COOKIE --output $PATH_TMP_FILE --data "password=${ROUTER_PASSWORD_MD5}&showpw=0&httoken=${TOKEN_LOGIN}" "http://${ROUTER_IP}/data/Login.json" > /dev/null
LOGIN_RESULT=$( more $PATH_TMP_FILE | python -c "import json,sys;obj=json.load(sys.stdin);print obj[0][\"varvalue\"]" )
if [[ "$LOGIN_RESULT" -ne 'ok' ]]; then echo 'Error: Login failed.' && exit 0; fi
echo "Extracting token from overview page..."
curl -s --cookie $PATH_COOKIE --output $PATH_TMP_FILE "http://${ROUTER_IP}/html/content/overview/index.html?lang=en" > /dev/null
TOKEN_ACTIONS=$(grep -oh '_httoken = [0-9]\{5,15\};' $PATH_TMP_FILE | tr -dc '0-9')
if ! [[ "$TOKEN_ACTIONS" =~ ^[0-9]+$ ]]; then echo 'Error: Extracting the token failed.' && exit 0; fi
echo "Disconnecting..."
curl -s -H "Accept: application/json" --cookie $PATH_COOKIE --data "req_connect=disabled&httoken=${TOKEN_ACTIONS}" --referer "http://speedport.ip/html/content/overview/index.html?lang=en" "http://${ROUTER_IP}/data/Connect.json" > /dev/null
sleep 3
echo "Connecting..."
curl -s -H "Accept: application/json" --cookie $PATH_COOKIE --data "req_connect=online&httoken=${TOKEN_ACTIONS}" --referer "http://speedport.ip/html/content/overview/index.html?lang=en" "http://${ROUTER_IP}/data/Connect.json" > /dev/null
sleep 3
rm $PATH_TMP_FILE
rm $PATH_COOKIE
echo "New IP: $(curl -s my-ip-is.appspot.com/plain)"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment