If you want to just get the value, you can use a simple shell oneliner like this:
S=10; F=/sys/class/net/eth0/statistics/rx_bytes; X=`cat $F`; sleep $S; Y=`cat $F`; BPS="$(((YX)/S))"; echo $BPS
It will show you the average βreceived bytes per secondβ for 10 seconds (you can change the period by changing the parameter S=10 , and you can measure the transmitted BPS instead of the received BPS, using tx_bytes instead of rx_bytes ). Remember to change eth0 to the network device that you want to monitor.
Of course, you are not limited to displaying average speed (as mentioned in other answers, there are other tools that will show you a much more pleasant result), but this solution can easily be scripted for other purposes.
For example, the following shell script (divided into several lines for readability) will execute the offlineimap process only if the average transfer speed of 5 minutes falls below 10 kBP (presumably when some other process with a bandwidth ends):
#!/bin/sh S=300; F=/sys/class/net/eth0/statistics/tx_bytes BPS=999999 while [ $BPS -gt 10000 ] do X=`cat $F`; sleep $S; Y=`cat $F`; BPS="$(((YX)/S))"; echo BPS is currently $BPS done offlineimap
Note that /sys/class/... is specific to Linux (this is normal since the sender chose the linux tag) and needs a non-archaic kernel. The shell code itself is compatible with / bin / sh (therefore, not only bash, but also dashes and other / bin / sh implementations), and / bin / sh is what is always installed.
Matija Nalis Sep 20 '14 at 13:10 2014-09-20 13:10
source share