Tuesday, March 14, 2006

Bandwidth Monitoring Script (through ifconfig)

Some ppl might need sumthing to monitor their bwidth … i think this script would b enough for some of us … i wrote it by myself by manipulating the output of ifconfig …



#!/bin/bash

IFCONFIG="/sbin/ifconfig”
IFACE=$1
if [ “${1}” != “” ]; then
IFOUTPUT=`${IFCONFIG} ${IFACE} |grep bytes`
DOWNOLD=`echo ${IFOUTPUT}|awk ‘{print $2}’|sed s/"bytes:"//`
UPOLD=`echo ${IFOUTPUT}|grep bytes|awk ‘{print $6}’|sed s/"bytes:"//`
sleep 0.8
IFOUTPUT=`${IFCONFIG} ${IFACE} |grep bytes`
DOWNNOW=`echo ${IFOUTPUT}|awk ‘{print $2}’|sed s/"bytes:"//`
UPNOW=`echo ${IFOUTPUT}|awk ‘{print $6}’|sed s/"bytes:"//`

let “UP= $UPNOW - $UPOLD”
let “DOWN= $DOWNNOW - $DOWNOLD”
let “TOTAL = UP + DOWN”
echo “—-KBps Bandwitdh Monitor—-”
echo “”
echo ” by KageSenshi”
echo “”
echo “”
echo “——————————”
echo “Bandwidth for Interface ${IFACE}”
echo “——————————”
echo “”
echo ” KBps MBps”
echo “Upload Speed $(($UP / 1024)) $(($UP / 1024 / 1024))”
echo “Download Speed $(($DOWN / 1024)) $(($DOWN / 1024 / 1024))”
echo ” Total $(($TOTAL / 1024)) $(($TOTAL / 1024 /1024))”
echo “”

# uncomment this if u want the output to be stored in ur hdd
# echo “${UP} ${DOWN} ${TOTAL}” >> $HOME/bwidth.txt

echo “”
echo “*Tips : View this in realtime”
echo ” watch -d=c bwmon ${IFACE} “
else
echo “Bandwitdh Monitor “
echo “By: KageSenshi “
echo ” “
echo “Usage:”
echo ” bwmon
echo ” watch -d=n bwmon
fi