|
How to get the current network bandwidth usage on Linux |
|
|
sysadmin
|
|
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: #!/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 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: [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 see also: http://www.madmadmod.com/sysadmin/62-monitor-network-interfaces-with-iftop.html
|
Thks for u're script: I gonna chge some lines to shutdown my computer when downloads are finished.
Bye