#!/bin/sh
DEBUG=1
LOGIN="user"
PASSWORD="passwort"
COOKIES="/tmp/.dynsdns.cookies.txt"
AL="en-gb"
UA="Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; Trident/6.0)"
LOGINURL="https://account.dyn.com/entrance/"
POSTURL="$LOGINURL"
CHKURL="https://account.dyn.com/"
[[ $DEBUG ]] && DST="-"
DST="/dev/null"
# random sleep time 1-3000
#DELAY=$(($(hexdump -n 2 -e '"%u"' /dev/urandom | tr -d '-' ) % 3000 + 1))
# If the method above didn't work, try the following method witk awk-srand.
# DELAY=$(awk 'BEGIN {srand(); print int (rand() * 3000) }')
#echo >&2 "Wait '$DELAY' sec..."
#sleep $DELAY
[[ -w "$( dirname "$COOKIES" )" ]]
{ echo >&2 "FATAL: Cannot write to directory '$( dirname "$COOKIES" )'" ; exit 1; }
# Ensure no broken session caching...
if [[ -s "$COOKIES" ]]; then
[[ -w "$COOKIES" ]]
{ echo >&2 "FATAL: Cannot write to file '$COOKIES'" ; exit 1 ; }
rm -f "$COOKIES" >/dev/null 2>&1
fi
[[ $DEBUG ]] && echo >&2 "DEBUG: Fetching initial headers to pre-load cookies..."
curl -b $COOKIES -c $COOKIES -Ikso "$DST" -A \"$UA\" --url "$LOGINURL"
[[ $DEBUG ]] && echo >&2 "DEBUG: Fetching UID..."
VALUE="$( curl -b $COOKIES -c $COOKIES -kso - -A "$UA" --url "$LOGINURL" | grep "multiform" | cut -d"'" -f 6 | head -n 1 )"
#VALUE="$(curl -b $COOKIES -c $COOKIES -kso - -A \"$UA\" --url "$LOGINURL" | \
# grep -m 1 "multiform" | \
# cut -d\' -f 6)"
[[ $DEBUG ]] && echo >&2 "DEBUG: Read UID as '$VALUE' - posting data..."
curl
-b $COOKIES -c $COOKIES -d "username=$LOGIN" -d "password=$PASSWORD" -d
"iov_id" -d "multiform=$VALUE" -e "$LOGINURL" -kso "$DST" -A \"$UA\"
--url "$POSTURL"
[[ $DEBUG ]] && echo >&2 "DEBUG: Response received - verifying result..."
curl -b $COOKIES -c $COOKIES -e "$POSTURL" -kso - -A \"$UA\" -H "Accept-Language: $AL" --url "$CHKURL" | \
grep -qE "<span>(Welcome|Hi) <b>$LOGIN</b></span>" \
&& { echo "Login successful" ; /sbin/write_log "DynDNS login successful" 4 ; } \
{ echo >&2 "Login failed" ; /sbin/write_log "DynDNS failed to login" 2;exit 1 ; }
exit 0