Skip to content

Instantly share code, notes, and snippets.

@Golit
Created February 5, 2015 04:52
Show Gist options
  • Save Golit/46e0eaf59ebb302da045 to your computer and use it in GitHub Desktop.
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
@joeduck
Copy link

joeduck commented Mar 7, 2015

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!)

    WLANURL="$IP/$KEY/userRpm/WlanStationRpm.htm"
    WLAN5GURL="$IP/$KEY/userRpm/WlanStationRpm_5g.htm"

    echo "Checking Clients 2.4 GHZ"
    RESPOND_WLAN=`curl -ss "$WLANURL" --cookie "${ENC_AUTH_COOKIE}" --referer "http://$IP/$KEY/userRpm/WlanStationRpm.htm"`
    echo "Checking Clients 5 GHZ"
    RESPOND_WLAN5G=`curl -ss "$WLANURL5G" --cookie "${ENC_AUTH_COOKIE}" --referer "http://$IP/$KEY/userRpm/WlanStationRpm.htm"`

    #Phone 1
    if [[ $RESPOND_WLAN =~ "78-99-99-99-99-99" || $RESPOND_WLAN5G =~ "78-99-99-99-99-99" ]]
    then
            wget -q -O /dev/null 'http://homematic.fritz.box/config/xmlapi/statechange.cgi?ise_id=1234&new_value=true' | echo $(date) $mac, Phone 1, angemeldet
    else
            wget -q -O /dev/null 'http://homematic.fritz.box/config/xmlapi/statechange.cgi?ise_id=1234&new_value=false' | echo $(date) $mac, Phone 1, abgemeldet        
    fi

@Github197
Copy link

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.

@AlphaC0de
Copy link

AlphaC0de commented Nov 29, 2020

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.

@Golit
Copy link
Author

Golit commented Nov 29, 2020

@AlphaC0de You are missing the md5sum program. Install the coreutils package for your distribution.

@AlphaC0de
Copy link

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

@alvid98
Copy link

alvid98 commented May 13, 2021

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

@AlphaC0de
Copy link

AlphaC0de commented May 13, 2021

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)

@AlphaC0de
Copy link

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