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..

Advertisements

Suse 11.1 Linux and Egypt Mobinil 3G USB Stick Modem

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


Yesterday, I went to Mobinil branch in Tanta, I wanned to buy a USB Stick modem. My issue was that I’m using Linux SUSE 11.1, and I know that the USB Stick modem support is for Windows and Mac OSs only. So, I wanned to know for sure.

There I asked a sales person called “Ahmed Salamah” ( who by the way was really cooporative ). He tested with me the stick modem on my laptop and called a friend of his ( may be a technical support for mobinil, I actually dunno ) who assured him that linux supports USB Stick modem of Type ” HUAWEI E156G” which they sell at Mobinil’s branch. But, it does not support the other model they sell.

So, we tried it several times, but no use. So, we googled for it. I found a link which is that

http://forums.opensuse.org/network-internet/422098-how-configure-usb-modem-opensuse-11-1-a.html

and the technical guy sent us another link on mail, which is this one

http://1337stuff4pro.blogspot.com/2008/05/huawei-e270230-for-linux-os.html

So, I bought the stick modem and said to myself ” Ok, as long as you know for sure that it works for linux, you find a way to make it work, at mobinil they are not the linux gurus”. I bought it and take it back home, tried to find a solution online, and I stuck it into my laptop.

And the surprise was, that the nm-applet gnome Network Manager applet, recognized the connection after a while. And it works fine.

My nm-applet versin in 0.7.0

I guess this is pretty much it.

Notes: This tick modem got a LED, when it is colored Stable Green then it is connected to GPRS
When connected to Light Blue, then it is connected to HSPD (Not sure of the name)  which is better.
When it is connected to Gark Blue, then it is connected to 3G which is the best.

One other thing to note. While we were testing the stick modem on Windows. We found that you can put a limit to your usage in HUAWEI E156G Stick modem in the configuration of the program that ships with it from mobinil. And it got better statistics and configuration than the other modems. So, I suggest that one.

I Dunno about lots of other modems, there may be better modems out there, but between the two types that we tried at mobinil ( the HUAWEI and HTC I guess ), I found this one better.

Posted in LINUX. Tags: , , , , , , . 2 Comments »

Mounting NTFS Drive ( Read, Write ) to all users.

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


Recently, I had the issue that my external HDD can’t be mounted as ( Read , Write ). It was getting mounted as read only for root user only. I needed to mount it as Read Write for all users.

The issue :
The HDD was not mounted properly , so that it needs to be fixed.

The solution:
connect the HDD ..
execute : dmesg

to know the HDD Block  ( e.g. /dev/sdb )
then execute the mount comand like this

mount -t ntfs-3g /dev/sdb1 /mnt/mybook/ -o rw,force

then unmount the device

umount /dev/sdb1

then mount again.

mount -t ntfs-3g /dev/sdb1 /mnt/mybook/ -o rw,force

Don’t forget to unmount the HDD after doing stuff with it.

and this is it …

Groovy Very Basic Statistics Tool

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


I am in a project where I am in charge of watching and reporting performance of a JEE Application on JBoss Application Server. Due to my usual need to caculate Average, Max, Min values of the data I observe. I made this little code to help me out ( Which for me it did help out ).

Here it is:

pagesLoad = [ 5.767,9.296, 3.814, 1.826, 2.889,8.000,6.797,2.926,5.992,2.430,16.191,14.294,2.778,7.037,4.745,5.004,4.601,6.930,2.275,2.896,14.319,3.836,11.059,2.295,2.845,7.078,6.194,1.812,14.811,4.105,3.013,16.358,4.368,3.314,21.828,6.188,8.137 ]
bitsRecieved = [ 27764, 253033, 921870, 142091, 307, 214406, 890962, 307, 307, 1259790, 553839, 749870, 143216, 146175, 179923, 517335, 572690, 180094, 801059, 143218, 215843, 215407, 146973, 572912, 801120, 587021, 239474, 518619, 307, 2409085, 908437, 16453, 143218, 215847, 218320, 1121673, 215163, 119930, 578828, 135629, 23098, 384, 143422, 215847, 215040, 254052, 578110, 216877, 921819 ]

def showStatistics(data,over=1,append=''){
    float min = 100000000
    float max = 0
    int count = data.size()
    float total = 0;
    data.each {
        it= it/over
        if(it>max){
            max = it;
        }
        if(it<min){
            min = it;
        }
        total+= it
    }
    println "MAX IS : $max $append"
    println "MIN IS : $min $append"
    println "Total is : $total $append"
    println "Count is : $count"
    println "Average IS: ${total/count} $append"
    println '-'*100
}

//showStatistics(pagesLoad,1,'Seconds')
showStatistics(bitsRecieved,1024,'KB')
Posted in GROOVY. Tags: . Leave a Comment »

Virus Removal Groovy Script

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


As I’m a Linux User since years ago. I faced the problem of NO Viruses at my PC, but tons of viruses on my Flash Memory ( USB Stick ), due to copying files to/from windows machines.

Actually, I was never affected by these viruses on my own, but I do NOT want to be a transportation facility for the virus to spread across other machines.

I noticed that there are two simple types of viruses that usually get attached to the memory stick.

  1. A .inf file and a .exe file that ofcourse got a strange name that I can visually detect and manually remove at linux.
  2. A .exe file gets the name of the super directory with the addition of the extension .exe. Which was usually easy to detect visually but hard to remove manually due to the amount of folders on the memory.

So, to fasten the process of  removing the second virus, I wrote this simple Groovy script ( which is in fact a reflection of  an old Java program that I created for the same virus years ago )

// Searchs for the .exe Virus that has the same name of its parent directory
checkVirus = { folder ->
    folder.eachFile{ file ->
        if(file.isDirectory()){
            checkVirus(file)
        }else if ("${folder.name}.exe" == file.name ){
            it.delete()
        }
    }
}
checkVirus (new File('/media/disk/'))

And this is it. Any enhancements or suggestions will be great.