Shell script to count number of Mega Bytes used in PPP connections

[ This Blog Was Moved To : http://www.diknows.com ]


Recently I bought, a USB Stick modem that is limited to 500 MB, after the 500 MB each 3 MB = 1 LE. For that I needed to create a counter to check my usage .. not to exceed the 500 Limit. so here it is :

#!/bin/bash

while :;
do t=`pppstats -a | grep -v IN | cut -c -10` ;
echo 'Created By Dino.'
echo '--------------------------------'
echo "Number Of Megabytes used is : ";
echo ${t}/1024/1024 | bc ;
echo '--------------------------------'
sleep 10;
clear;
done

And This is it .. I used till now 166 MBs.. I like that..

======================

New Updates : Lets call it version : 0.2

——————–

#!/bin/bash
###########################################################
#
#        Created By Diaa Mohamed Kasem - Fall 2009
#
###########################################################
#The Main Directory, 
#where ppp counter related data are stored.
mainDir="/root/.pppCounter"
#The Data File where the latest bytes count are stored
dataFile="$mainDir/data.txt"
#The temporary variable holding the ppp IN bytes count
currentValue=0
#Create the main directory if it doesn't exist
mkdir -p $mainDir

# If the data file exists
if [ -f $dataFile ]; then
  # Get the latest saved bytes count 
  currentValue=`cat $dataFile`
  # Inform User
  echo "Latest Used Value is : $(($currentValue/1024/1024))"
fi

# Infinit loop
while :; 
  # Get the current bytes count downloaded 
  # since injecting the stick modem
  do t=`pppstats -a | grep -v IN | cut -c -10` ;
  # Inform User
  echo 'Created By Dino.'
  echo '--------------------------------'
  echo "Number Of Megabytes used is : \
  $(($t/1024/1024 + $currentValue/1024/1024 )) MB";
  echo '--------------------------------'
  # Save the data in the data file
  echo $(($t+$currentValue)) > $dataFile
  # Sleep for 10 seconds
  sleep 10;
  # Clear the screen
  clear;
done

Note : This should run as root

And here is the result till now :

Created By Dino.
——————————–
Number Of Megabytes used is :   97 MB
——————————–

Hope it is helpful..