#!/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]>|_