Sep 4, 2010

ADSL Usage checker update

I took some time today to revamp the ADSL usage checking script I wrote some time ago and added a few more features like the ability to output more detailed information.

I added a few more options like the ability to customize the output string, check for updates, and so on and so forth. Here's the updated script. It's a bit lengthy...

#!/bin/bash
##################################################
# Ugly Dirty yet functional script to check ADSL
# usage statistics for Dhiraagu subscribers.
# 
# type ./dslusage -h for options
#
# This script is provided as is, without liabilities
# or guarantee.
#
# 2010 KudaNai - http://kudanai.blogspot.com
#-------------------------------------------------


username= #your login name
password= #your password here
package_allowance=20 #must be specified
version=0.2

#End of configuration section.
PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
fmtstring="DSL Usage:\tGBUSEDGb / GBALLOWGb ( GBLEFTGb remaining)" #Default format string

function pusage() {
 echo -e "\tUSEAGE: ${0} "
 cat ${0} | tail -n 23 | sed "s/#//g;s/VERSION/${version}/g"
 exit
}

function error() {
 echo -e "ERROR: $1\n"
 if [ -z $2 ];then pusage;fi
 exit
}
function checkupdate() {
 if [ -z $(which curl) ];then error "This functionality requires cURL. Please install it and try again" 1;fi
 local ver=$(curl --silent http://dl.dropbox.com/u/5385653/ddslcheck/ver)
 if [ $(expr ${ver} \> ${version}) -eq 1 ]
 then 
  echo "A newer version ${ver} is available online."
 else
  echo "You are probably using the latest version."
 fi
}

while getopts 'vVUhu:p:f:' OPTION
do
 case $OPTION in
  u) username=${OPTARG};;
  p) password=${OPTARG};;
  a) package_allowance=${OPTARG};;
  v) fmtstring="Dhiraagu ADSL Usage Details as on\n`date`\n\nCustomer:\tSUBSCRIBER (USERID)\nPackage:\tPACKAGE_NAME\nusage period:\tBILLING_START to BILLING_END (BILLING_CYCLE)\n\nDSL Usage:\tGBUSEDGb / GBALLOWGb ( GBLEFTGb remaining)";;
  V) cat $0 | tail -n 5 | sed "s/#//g;s/VERSION/${version}/g";exit;;
  f) fmtstring=${OPTARG};;
  U) checkupdate;exit;;
  h) pusage;;
  ?) pusage;;
 esac
done

if [ -z $(which img2txt) ]; then error "You need to install caca-utils before proceeding" 1;exit;fi
if [ -z $(which wget) ];then error "You don't have wget installed...seriously?" 1;exit;fi
if [[ -z ${username} || -z ${password} || -z ${package_allowance} ]]; then error "You need to specify username and password";fi

cd /tmp # ---------------here on down everything is done inside temp
wget --quiet --no-cache --post-data "web_user=${username}&web_pass=${password}" -p http://dsl.dhivehinet.net.mv/login.php
if [ ! -f "dsl.dhivehinet.net.mv/comparision_graph.php" ];then error "Invalid username/password" 1;fi

OIFS="{$IFS}"; IFS=$'\n'
count=0
for LINE in $(sed -n 's/.*"#000080" size="2">\([^<]*\).*/\1/p' dsl.dhivehinet.net.mv/topframe.php;sed -n 's/.*through \(.*\) to \([^<]*\)<.*/\1\n\2/p' dsl.dhivehinet.net.mv/comparision_tpl.php)
do
 ARR[${count}]=$LINE
 ((count++))
done
IFS="${OIFS}"

gstring=$(img2txt -H 400 -W 4 -d none -f ansi dsl.dhivehinet.net.mv/comparision_graph.php)
gtotal=$(echo "${gstring}" | grep 44 | wc -l)
gused=$(echo "${gstring}" | grep 43 | wc -l)
gbused=$(echo "scale=3;${gused} / ${gtotal} * ${package_allowance}" | bc -l)
gbleft=$(echo "scale=3;${package_allowance} - ${gbused}" | bc -l)

echo -e "${fmtstring}" | sed "s/SUBSCRIBER/${ARR[0]}/g;s/USERID/${ARR[1]}/g;s/PACKAGE_NAME/${ARR[2]}/g;s/BILLING_CYCLE/${ARR[3]}/g;s/BILLING_START/${ARR[4]}/g;s/BILLING_END/${ARR[5]}/g;s/GBUSED/${gbused}/g;s/GBLEFT/${gbleft}/g;s/GBALLOW/${package_allowance}/g;"

rm -r dsl.dhivehinet.net.mv
#
#OPTIONS:
# -a Specify package allowance for limited connections.
# -f Define custom format string.
# -h Print this help.
# -p Specify password.
# -u Specify username.
# -U Check for updates.
# -v Verbose output. Print more details.
# -V print version information and exit.
#
#FORMATTING:
# The format string can by anything containing any of the following
# special variables which will be substituted with appropriate values.
# use \n,\t for newlines and tabs.
#
# SUBSCRIBER,USERID,PACKAGE_NAME,BILLING_CYCLE 
# BILLING_START,BILLING_END,GBUSED,GBLEFT,GBALLOW
#
# Dhiraagu DSLUsage checker
# 2010 - KudaNai [ http://kudanai.blogspot.com ]
# vVERSION
#

10 Comments:

Anonymous said...

Thanks for the update.

Anonymous said...

wow..overkill

Anonymous said...

caca-utils should be installed before running this script if your running a debian based distro

Soul

SoE said...

Thanks for pointing that out Soul..I forgot to mention that in the post.

Is it working for you?

Anonymous said...

from wher i can get caca-utils

SoE said...

it should be available for most distributions in their default repo's. If not, a quick google search should turn it up.

after installing that, you can run

./script -u USERNAME -p PASSWORD -a 20 -v

where 20 indicates that your package's max. allowance is 20Gb (this is important.)

if you don't want to specify these every time, you can change the values for the appropriate variables inside the script.

Anonymous said...

thanks bro

Anonymous said...

is thr any way that i can contact u?

SoE said...

sure. You can catch me on twitter/facebook or email me at anything [] kudanai.com

SoE

Anonymous said...

What's up, I check your blog on a regular basis. Your writing style is awesome, keep up the good work!

My blog; cccam server

Post a Comment