Linux/Unix Network Bandwidth Script

Posted: May 3, 2011 in CLI, Linux

Hello Guys,

I just finished a very funny script to get the current bandwidth utilization of a Linux/Unix ethernet card.
This is just the first draft I have to make tons of changes, and options. but this is the main body the first working copy.

As always any comments are appreciated.

#!/bin/bash

#Function to get bit sample
get_sample()
{
/sbin/ifconfig eth0 | grep 'RX bytes:' | awk ' { split($2, RX, ":"); print RX[2]}'
}

#Function to calculate the difference between 2 samples
calc_sample()
{
A=$(get_sample)
sleep 1
B=$(get_sample)
echo $(($B - $A))
}

#Function to make the output more human readable
humanize()
{
if [ $((($RESULT * 8)/1000)) -gt 1000 ]
then
echo 0
else
echo 1
fi
}

#Main script
while true; do
RESULT=$(calc_sample)
#This checks if the output is readable or not
if [ $(humanize) -eq 0 ]
then
echo $(((($RESULT * 8)/1000)/10000))
echo "Megabits per second"
else
echo $((($RESULT * 8)/1000))
echo "Kilobits per second"
fi
#Sleep 5 secs before asking for new sample
sleep 5
done

Network Speed

Network Speed

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s