-
-
Save Golit/46e0eaf59ebb302da045 to your computer and use it in GitHub Desktop.
#!/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 |
Also thanks from my side! I had some issue with firmware version TL-WDR3600_V1_150518! Turns out the --referer needs to have the same URL as the one requested for all URLs opening after successful login.
I am using this script to reboot my router:
TPURL="$IP/$KEY/userRpm/SysRebootRpm.htm?reboot=Reboot"
RESPOND=`curl -ss "$TPURL" --cookie "${ENC_AUTH_COOKIE}" --referer "$TPURL"`
One more thing. In your script you should logout at the end. At least with my firmware version it is otherwise not possible to login to the router via browser for some time. There is then the message in the browser show, that it is only one administrator allowed to login.
Also thanks from my side! I had some issue with firmware version TL-WDR3600_V1_150518! Turns out the --referer needs to have the same URL as the one requested for all URLs opening after successful login.
I am using this script to reboot my router:TPURL="$IP/$KEY/userRpm/SysRebootRpm.htm?reboot=Reboot" RESPOND=`curl -ss "$TPURL" --cookie "${ENC_AUTH_COOKIE}" --referer "$TPURL"`
One more thing. In your script you should logout at the end. At least with my firmware version it is otherwise not possible to login to the router via browser for some time. There is then the message in the browser show, that it is only one administrator allowed to login.
@Github197 I have the same firmware version as your and I cannot properly use that script; may you help me? Where I have to change those lines?
EDIT: and it says me:
/tplink-reconnect.sh: line 52: md5sum: command not found
Login failed!
You could have executed the script too often in a short period! In this case try again in 2h.
@AlphaC0de You are missing the md5sum program. Install the coreutils package for your distribution.
Is this updated script correct (FW ver, TL-WDR3600_V1_150518)?
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/SysRebootRpm.htm?reboot=Reboot"
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
Many Thanks for this. Have it modified and used to list connected WLAN Clients for Presence Detection in my Homematic Home Automation System. Here ist the modified code ( sorry for the bad quality, i am not a coder!)