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
@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