#!/bin/sh OP="$1" SERVICE="$2" USERNAME="$3" PASSWORD="$4" MONTHSAGO="$5" DAYSAGO="$5" # uncomment the following line if usage is accounted for on "per minute or part thereof" basis TOTALIZER='{totalsecs +=(3600 * $1 + 60 * ($2 + int(($3 + 59)/60)))} END {print totalsecs/60}' # uncomment the following line if usage is accounted for on per second basis #TOTALIZER='{totalsecs += (3600 * $1 + 60 * $2 + $3)} END {print int((totalsecs+59)/60)}' usage() { echo >&2 "usage: $(basename $0) [creditcents|usedminutes|last7daysminutes|remainingdays] service username password [monthsago|daysago]" exit 255 } Curl() { curl "$@" local STATUS="$?" if [ ! $STATUS ]; then rm -f ${COOKIEJAR}; exit $STATUS; fi } case _"$OP" in _creditcents) ;; _remainingdays) ;; _usedminutes) ;; _last7daysminutes) NOW=$(awk 'BEGIN{print systime()}') if [ $? != 0 ]; then echo >&2 "This script requires awk installed". rm -f ${COOKIEJAR}; exit 255 fi ;; *) usage ;; esac case _"$PASSWORD" in _) usage ;; esac gomainpage() { LOGGEDIN=$(Curl -s -k -b ${COOKIEJAR} \ "${BASEURL}"'/index.php?part=menu&justloggedin=true') } BASEURL="https://myaccount.${SERVICE}/clx" COOKIEJAR="/tmp/cj.$$" CALLRECORDS="/tmp/cr.$$" umask 077 # protect cookie jar from indiscreet eyes #### Login with username and password LOGGEDIN=$(Curl -s -k -c ${COOKIEJAR} -d username="${USERNAME}" -d password="${PASSWORD}" ${BASEURL}/ | grep -i 'Refresh') if [ _"$LOGGEDIN" = "_" ]; then rm -f ${COOKIEJAR}; exit 254; fi #### go to index page as required by META Refresh. This time will fail (?) gomainpage case _"$OP" in _creditcents) #### go to index page as required by META Refresh. This time will work (?) gomainpage # Thanks to Laurent Lesage for "LC_ALL=en_US" to ensure # that awk will treat '.' as decimal point regardless of locale echo "$LOGGEDIN" \ | sed -n -e '/emaining credit/N;s|.*emaining credit[^0-9]\+\([0-9.]\+\).*|\1|p' \ | LC_ALL=en_US awk '{print 100 * $1}' ;; _remainingdays) #### go to index page as required by META Refresh. This time will work (?) gomainpage echo "$LOGGEDIN" \ | sed -n -e '/[rR]emaining:/N;s|.*[rR]emaining:[^1-9]*\([1-9][0-9]*\).*|\1|p' ;; _usedminutes) CURYEAR=$(date +%Y) CURMONTH=$(date +%m | sed -e 's/^0//') if [ _"$MONTHSAGO" != _ ]; then CURABSMONTH=$((12 * $CURYEAR + $CURMONTH - 1)) THENABSMONTH=$(($CURABSMONTH - $MONTHSAGO)) CURYEAR=$(($THENABSMONTH / 12)) CURMONTH=$((($THENABSMONTH % 12) + 1)) fi Curl -s -k -b ${COOKIEJAR} "${BASEURL}/calls.php?month=${CURMONTH}&year=${CURYEAR}" \ | sed -e '/FREE/s|<[tT][rR]>|_|g' | tr '_' '\n' | sed -n -e \ '/FREE/s|[[:space:]]*.*\([0-9][0-9]\):\([0-9][0-9]\):\([0-9][0-9]\).*FREE.*|\1 \2 \3|p' \ | awk "$TOTALIZER" #### go to index page before logoff (?) gomainpage ;; _last7daysminutes) datedaysago() { # format days_ago [timestamp] local TS="$3" if [ _"$TS" = _ ]; then TS=$(awk 'BEGIN{print systime()}'); fi awk 'BEGIN{print strftime("'"$1"'",'"$TS"'-('"$2"'*24*3600))}' } if [ _"$DAYSAGO" = _ ]; then DAYSAGO=0 fi YEAREND=$(datedaysago "%Y" "$DAYSAGO" "$NOW") ZMONTHEND=$(datedaysago "%m" "$DAYSAGO" "$NOW") ZDAYEND=$(datedaysago "%d" "$DAYSAGO" "$NOW") MONTHEND=$(echo "$ZMONTHEND" | sed -e 's/^0//') DAYEND=$(echo "$ZDAYEND" | sed -e 's/^0//') YEARSTART=$(datedaysago "%Y" "$(($DAYSAGO + 6))" "$NOW") ZMONTHSTART=$(datedaysago "%m" "$(($DAYSAGO + 6))" "$NOW") ZDAYSTART=$(datedaysago "%d" "$(($DAYSAGO + 6))" "$NOW") MONTHSTART=$(echo "$ZMONTHSTART" | sed -e 's/^0//') DAYSTART=$(echo "$ZDAYSTART" | sed -e 's/^0//') { { if [ _"$MONTHSTART" != _"$MONTHEND" ]; then #### get last month's data Curl -s -k -b ${COOKIEJAR} \ "${BASEURL}/calls.php?month=${MONTHSTART}&year=${YEARSTART}" #### go to index page before getting this month's data (?) gomainpage fi Curl -s -k -b ${COOKIEJAR} \ "${BASEURL}/calls.php?month=${MONTHEND}&year=${YEAREND}" } \ | sed -e '/FREE/s|<[tT][rR]>|_|g' | tr '_' '\n' | sed -n -e \ '/FREE/s|[[:space:]]*.*\([0-9]\{4\}\)-\([0-9]\{2\}\)-\([0-9]\{2\}\).*\([0-9][0-9]\):\([0-9]\{2\}\):\([0-9]\{2\}\).*FREE.*|\1 \2 \3 \4 \5 \6|p' \ | while read YEAR MONTH DAY DURH DURM DURS; do if [ $YEAR$MONTH$DAY -ge $YEARSTART$ZMONTHSTART$ZDAYSTART -a $YEAR$MONTH$DAY -le $YEAREND$ZMONTHEND$ZDAYEND ]; then echo "$DURH $DURM $DURS" fi done } | awk "$TOTALIZER" #### go to index page before logoff (?) gomainpage ;; esac LOGGEDIN=$(Curl -s -k -b ${COOKIEJAR} "${BASEURL}/index.php?part=logoff") rm -f ${COOKIEJAR}