darkcloud-tmuxconfig/bwrate
2015-02-27 09:22:10 -05:00

26 lines
574 B
Bash
Executable file

#!/usr/bin/env bash
timespan=1
[[ -z "$1" ]] && {
printf '%s\n' 'Error: no device specified'
exit 1
}
[[ ! -d "/sys/class/net/$1" ]] && {
printf '%s\n' "No such device: $1"
exit 1
}
br1=$(</sys/class/net/"$1"/statistics/rx_bytes)
bt1=$(</sys/class/net/"$1"/statistics/tx_bytes)
sleep $timespan
br2=$(cat /sys/class/net/"$1"/statistics/rx_bytes)
bt2=$(cat /sys/class/net/"$1"/statistics/tx_bytes)
u_speed=$(( ( ( bt2 - bt1 ) / timespan ) / 1024 ))
d_speed=$(( ( ( br2 - br1 ) / timespan ) / 1024 ))
printf '%s\n' "D ${d_speed}KB/s | U ${u_speed}KB/s"