Created
February 5, 2015 04:52
-
-
Save Golit/46e0eaf59ebb302da045 to your computer and use it in GitHub Desktop.
This script will reconnect your TP-Link TL-WDR3600 router. The script works with the latest firmware
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# The MIT License (MIT) | |
# | |
# Copyright (c) 2015 | |
# | |
# Permission is hereby granted, free of charge, to any person obtaining a copy | |
# of this software and associated documentation files (the "Software"), to deal | |
# in the Software without restriction, including without limitation the rights | |
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
# copies of the Software, and to permit persons to whom the Software is | |
# furnished to do so, subject to the following conditions: | |
# | |
# The above copyright notice and this permission notice shall be included in all | |
# copies or substantial portions of the Software. | |
# | |
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
# SOFTWARE. | |
# | |
# Propose of the script: | |
# | |
# This script will reconnect your TP-Link Router. if node.js is installed the | |
# new ip will be printed to stdout | |
# tested with: | |
# * TL-WDR3600 (fw: 3.14.1 Build 141022 Rel.33470n) | |
# (it may work with other TP-Link router with similar firmware) | |
# | |
# WARNING: Even if you login with correct username and password the router may lock | |
# the loggin if your run the script ten times in a short period. | |
# | |
# Dependency: bash, md5sum, awk, base64, perl, curl | |
# Optional dependency: node.js | |
# | |
# Windows user may take a look at MinGW or Cygwin | |
# | |
# usage: ./tplink-reconnect.sh [<user> [<password> [<ip>]]] | |
# or just modify the values directly in this file. | |
# If you did not modifed the login you can run the script without any argument | |
# | |
IP=${3:-"tplinkwifi.net"} | |
USER=${1:-"admin"} | |
PASSWORD=${2:-"admin"} | |
PASSWORD=`echo -n "${PASSWORD}" | md5sum | awk '{ print $1 }'` | |
auth=`echo -n "${USER}:${PASSWORD}" | base64` | |
AUTH="Basic $auth" | |
ENC_AUTH_COOKIE="Authorization="`perl -MURI::Escape -e 'print uri_escape($ARGV[0]);' "${AUTH}"` | |
RESPOND=`curl -ss "${IP}/userRpm/LoginRpm.htm?Save=Save" --cookie "${ENC_AUTH_COOKIE}"` | |
re="http\://[0-9A-Za-z.]+/([A-Z]+)/userRpm/Index.htm" | |
if [[ $RESPOND =~ $re ]] | |
then | |
echo 'Login succesfully' | |
KEY=${BASH_REMATCH[1]} | |
DISCONNECTURL="$IP/$KEY/userRpm/StatusRpm.htm?Disconnect=Disconnect&wan=1" | |
CONNECTURL="$IP/$KEY/userRpm/StatusRpm.htm?Connect=Connect&wan=1" | |
echo "disconnecting WAN ..." | |
curl -ss "$DISCONNECTURL" --cookie "${ENC_AUTH_COOKIE}" --referer "http://$IP/$KEY/userRpm/StatusRpm.htm" > /dev/null | |
echo "connecting WAN ..." | |
RESPOND=`curl -ss "$CONNECTURL" --cookie "${ENC_AUTH_COOKIE}" --referer "http://$IP/$KEY/userRpm/StatusRpm.htm"` | |
if [ `which node 2> /dev/null` ] | |
then | |
echo -n "Waiting for new ip " | |
WANIP="0.0.0.0" | |
while [ $WANIP == "0.0.0.0" ] | |
do | |
RESPOND=`curl -ss "$IP/$KEY/userRpm/StatusRpm.htm" --cookie "${ENC_AUTH_COOKIE}" --referer "http://$IP/$KEY/userRpm/StatusRpm.htm"` | |
re="(var\swanPara.+)</SCRIPT>\s<META" | |
if [[ $RESPOND =~ $re ]] | |
then | |
info=${BASH_REMATCH[1]} | |
WANIP=`node -e "$info console.log(wanPara[2]);"` | |
fi | |
echo -n "." | |
sleep 1 | |
done | |
echo "" | |
echo "Your IP: $WANIP" | |
fi | |
exit 0 | |
else | |
echo 'Login failed!' | |
echo 'You could have executed the script too often in a short period! In this case try again in 2h.' | |
fi | |
exit -1 |
I edit the script to restart it:
IP=${3:-"192.168.1.1"}
USER=${1:-"user"}
PASSWORD=${2:-"password"}
PASSWORD=`echo -n "${PASSWORD}" | md5sum | awk '{ print $1 }'`
auth=`echo -n "${USER}:${PASSWORD}" | base64`
AUTH="Basic $auth"
ENC_AUTH_COOKIE="Authorization="`perl -MURI::Escape -e 'print uri_escape($ARGV[0]);' "${AUTH}"`
RESPOND=`curl -ss "${IP}/userRpm/LoginRpm.htm?Save=Save" --cookie "${ENC_AUTH_COOKIE}"`
re="http\://[0-9A-Za-z.]+/([A-Z]+)/userRpm/Index.htm"
if [[ $RESPOND =~ $re ]]
then
echo 'Login succesfully'
KEY=${BASH_REMATCH[1]}
REBOOTURL="$IP/$KEY/userRpm/SysRebootRpm.htm?Reboot=Reboot"
echo "Rebooting ..."
curl -ss "$REBOOTURL" --cookie "${ENC_AUTH_COOKIE}" --referer "http://$IP/$KEY/userRpm/StatusRpm.htm" > /dev/null
exit 0
else
echo 'Login failed!'
echo 'You could have executed the script too often in a short period! In this case try again in 2h.'!'
fi
exit -1
I edit the script to restart it:
IP=${3:-"192.168.1.1"} USER=${1:-"user"} PASSWORD=${2:-"password"} PASSWORD=`echo -n "${PASSWORD}" | md5sum | awk '{ print $1 }'` auth=`echo -n "${USER}:${PASSWORD}" | base64` AUTH="Basic $auth" ENC_AUTH_COOKIE="Authorization="`perl -MURI::Escape -e 'print uri_escape($ARGV[0]);' "${AUTH}"` RESPOND=`curl -ss "${IP}/userRpm/LoginRpm.htm?Save=Save" --cookie "${ENC_AUTH_COOKIE}"` re="http\://[0-9A-Za-z.]+/([A-Z]+)/userRpm/Index.htm" if [[ $RESPOND =~ $re ]] then echo 'Login succesfully' KEY=${BASH_REMATCH[1]} REBOOTURL="$IP/$KEY/userRpm/SysRebootRpm.htm?Reboot=Reboot" echo "Rebooting ..." curl -ss "$REBOOTURL" --cookie "${ENC_AUTH_COOKIE}" --referer "http://$IP/$KEY/userRpm/StatusRpm.htm" > /dev/null exit 0 else echo 'Login failed!' echo 'You could have executed the script too often in a short period! In this case try again in 2h.'!' fi exit -1
It says me
line 26: unexpected EOF while looking for matching `''
line 30: syntax error: unexpected end of file
and line 26 is
echo 'You could have executed the script too often in a short period! In this case try again in 2h.'!'
and line 30 doesn't exits (29 is exit -1)
Found a typo; however, deleting > /dev/null to see what it runs it says
Login succesfully
Rebooting ...
<hr><h1><B>You have no authority to access this router!</B></h1><hr>%
IP=${3:-"192.168.1.1"}
USER=${1:-"user"}
PASSWORD=${2:-"password"}
PASSWORD=`echo -n "${PASSWORD}" | md5sum | awk '{ print $1 }'`
auth=`echo -n "${USER}:${PASSWORD}" | base64`
AUTH="Basic $auth"
ENC_AUTH_COOKIE="Authorization="`perl -MURI::Escape -e 'print uri_escape($ARGV[0]);' "${AUTH}"`
RESPOND=`curl -ss "${IP}/userRpm/LoginRpm.htm?Save=Save" --cookie "${ENC_AUTH_COOKIE}"`
re="http\://[0-9A-Za-z.]+/([A-Z]+)/userRpm/Index.htm"
if [[ $RESPOND =~ $re ]]
then
echo 'Login succesfully'
KEY=${BASH_REMATCH[1]}
REBOOTURL="$IP/$KEY/userRpm/SysRebootRpm.htm?Reboot=Reboot"
echo "Rebooting ..."
curl -ss "$REBOOTURL" --cookie "${ENC_AUTH_COOKIE}" --referer "$REBOOTURL"
exit 0
else
echo 'Login failed!'
echo 'You could have executed the script too often in a short period! In this case try again in 2h.'
fi
exit -1
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Is this updated script correct (FW ver, TL-WDR3600_V1_150518)?