Company Logo
Linux Sysadmin stuff

How to get the current network bandwidth usage on Linux

There are many ways how to get the current bandwidth usage on Linux. This is a very simple shell script that lists the network statistic every second:

 

{chilicode}

#!/bin/bash


if [ -z "$1" ]; then
        echo
        echo usage: $0 network-interface
        echo
        echo e.g. $0 eth0
        echo
        exit
fi

IF=$1

while true
do
        R1=`cat /sys/class/net/$1/statistics/rx_bytes`
        T1=`cat /sys/class/net/$1/statistics/tx_bytes`
        sleep 1
        R2=`cat /sys/class/net/$1/statistics/rx_bytes`
        T2=`cat /sys/class/net/$1/statistics/tx_bytes`
        TBPS=`expr $T2 - $T1`
        RBPS=`expr $R2 - $R1`
        TKBPS=`expr $TBPS / 1024`
        RKBPS=`expr $RBPS / 1024`
        echo "tx $1: $TKBPS kb/s rx $1: $RKBPS kb/s"
done

{/chilicode}

 

All you have to do is copy and paste this into a file (I named it /usr/local/bin/netspeed) and make it 755 (chmod 755 /usr/local/bin/netspeed) and start it with netspeed eth0:

 

{chilicode}

[root@blackmod bin]# netspeed eth0
tx eth0: 1 kb/s rx eth0: 328 kb/s
tx eth0: 0 kb/s rx eth0: 129 kb/s
tx eth0: 0 kb/s rx eth0: 68 kb/s
tx eth0: 0 kb/s rx eth0: 116 kb/s
tx eth0: 0 kb/s rx eth0: 138 kb/s
tx eth0: 0 kb/s rx eth0: 110 kb/s
tx eth0: 1 kb/s rx eth0: 153 kb/s
tx eth0: 0 kb/s rx eth0: 116 kb/s
tx eth0: 1 kb/s rx eth0: 115 kb/s
tx eth0: 0 kb/s rx eth0: 98 kb/s
tx eth0: 0 kb/s rx eth0: 106 kb/s
tx eth0: 1 kb/s rx eth0: 156 kb/s
tx eth0: 1 kb/s rx eth0: 117 kb/s
tx eth0: 0 kb/s rx eth0: 90 kb/s
tx eth0: 7 kb/s rx eth0: 490 kb/s
tx eth0: 1 kb/s rx eth0: 500 kb/s
tx eth0: 0 kb/s rx eth0: 83 kb/s
 

{/chilicode}

 

see also: http://www.madmadmod.com/sysadmin/62-monitor-network-interfaces-with-iftop.html

 

 

Comments   

 
0 #4 m1k4 2013-03-26 16:39
Thx, very usefull :-)
Quote
 
 
+1 #3 PSI 2012-11-13 07:26
Great script. You have to correct one thing: you get bytes from the sys/class so at the end you have to multiply it by 8 to have correct bits value.
Quote
 
 
0 #2 Sam 2012-06-15 15:23
Quite helpful. Thank you!
Quote
 
 
0 #1 croote 2010-08-30 07:19
Hello,
Thks for u're script: I gonna chge some lines to shutdown my computer when downloads are finished.

Bye
Quote
 

Add comment


Security code
Refresh

Main Menu




Powered by Joomla!®. Developed by: premium drupal templates ThemZa  Valid XHTML and CSS.