From b17d70f6e7eaca407a67712909590474e5e7aa86 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florian=20L=C3=B6ffler?= Date: Tue, 5 May 2020 11:09:30 +0200 Subject: [PATCH] started rewrite and cleanup + implemented retrieval of WAN IP without login --- easyBoxCtrl.sh | 62 ++++++++++++++++++++++++++++++++++---------------- 1 file changed, 43 insertions(+), 19 deletions(-) diff --git a/easyBoxCtrl.sh b/easyBoxCtrl.sh index 7680963..d0bd8fc 100755 --- a/easyBoxCtrl.sh +++ b/easyBoxCtrl.sh @@ -14,29 +14,58 @@ HOST="192.168.0.2" # The reconnect duration of the EasyBox 804 is quite long, it can be up to 180 seconds. +# fake user agent +UA="Mozilla/5.0" # FIXME spaces won't work because of bash parameter handling +COOKIEFILE="/tmp/easyBoxCookies.txt" +WGET_OPTS=( + "--quiet" + "--load-cookies $COOKIEFILE" + "--save-cookies $COOKIEFILE" + "--keep-session-cookies" + "--user-agent=\"${UA}\"" + "-O -" +) +WGET="/usr/bin/wget ${WGET_OPTS[@]}" + # * * * NO MORE CHANGES DOWN HERE * * * -read_dom () { - local IFS=\> - read -d \< ENTITY CONTENT -} +RETVAL="" +touch $COOKIEFILE -if [ "1" == "1" ]; then # Step 1: before sending the password - curl -s "http://${HOST}/main.cgi?page=login.html" -c step1_cookie.txt \ - -o step1_answer.html +echo "request: http://${HOST}/main.cgi?page=login.html" 1>&2 +OUT=$($WGET "http://${HOST}/main.cgi?page=login.html" ) + +DM_COOKIE=$(echo $OUT | grep dm_cookie | awk -F"dm_cookie='" '{ print $2 }' | awk -F"'" '{ print $1 }') +echo "DM_COOKIE = "${DM_COOKIE} 1>&2 + +WBM_COOKIE=$(cat $COOKIEFILE | grep "wbm_cookie_session_id" | awk -F"\t" '{ print $7 }') +echo "WBM_COOKIE = "${WBM_COOKIE} 1>&2 + +# Create a 'special' cookie file without HOST and PATH (important): +# cat step1_cookie.txt | sed "s/${HOST}//g" | sed 's/\t\/\t/\t\t/g' > step1_cookie_special.txt +#cat step1_cookie_special.txt + +# get WAN IP without login +if [[ "$1" == "wan" && "$2" == "ip" ]]; then + echo "request: http://${HOST}/data_model.cgi" 1>&2 + POST_DATA="${DM_COOKIE}1InternetGatewayDevice.WANDevice.6.WANConnectionDevice.4.WANPPPConnection.1.ExternalIPAddress" + + OUT=$($WGET \ + --post-data="${POST_DATA}" \ + "http://${HOST}/data_model.cgi") + + RETVAL=$(echo "$OUT" | xmllint --xpath "//Value/text()" -) +fi + +echo "$RETVAL" + +exit 0 - COOKIE_STEP1=$(cat step1_cookie.txt | grep "wbm_cookie_session_id" | awk -F"\t" '{ print $7 }') - echo "COOKIE_STEP1 = "${COOKIE_STEP1} - # Create a 'special' cookie file without HOST and PATH (important): - cat step1_cookie.txt | sed "s/${HOST}//g" | sed 's/\t\/\t/\t\t/g' > step1_cookie_special.txt - #cat step1_cookie_special.txt - DM_COOKIE_STEP3_STEP1=$(cat step1_answer.html | grep dm_cookie | awk -F"dm_cookie='" '{ print $2 }' | awk -F"'" '{ print $1 }') - echo "DM_COOKIE_STEP3_STEP1 = "${DM_COOKIE_STEP3_STEP1} # Sending cookies at this step is very important. # If no cookies are sent, a 'new auth_key' is generated, not matching the cookies stored by this bash script. @@ -64,11 +93,6 @@ if [ "1" == "1" ]; then # Step 1: before sending the password #cat step2_answer.html -fi - - - - if [ "1" == "1" ]; then # Step 3: After sending the password COOKIE_STEP3=$(cat step2_cookies.txt | grep "wbm_cookie_session_id" | awk -F"\t" '{ print $7 }') echo "COOKIE_STEP3 = "${COOKIE_STEP3}