2013-11-14 16:43:26 +01:00
|
|
|
#!/bin/sh
|
|
|
|
|
2014-01-04 14:45:55 +01:00
|
|
|
if [ "$1" = "-h" ] || [ $# -gt 1 ]; then
|
2014-11-12 16:15:39 +01:00
|
|
|
cat<<EOF
|
2013-11-14 16:43:26 +01:00
|
|
|
Usage: ${0##*/} [INTERFACE]
|
|
|
|
|
|
|
|
Return up and down transmission speed on given interface. If not given, use the
|
2014-01-04 14:45:55 +01:00
|
|
|
first online interface returned by ifconfig (lo is ignored).
|
2013-11-14 16:43:26 +01:00
|
|
|
EOF
|
2014-11-12 16:15:39 +01:00
|
|
|
exit
|
2013-11-14 16:43:26 +01:00
|
|
|
fi
|
|
|
|
|
2014-11-25 01:23:36 +01:00
|
|
|
if [ -n "$1" ]; then
|
|
|
|
interface="$1"
|
2013-11-14 16:43:26 +01:00
|
|
|
else
|
2014-11-25 01:23:36 +01:00
|
|
|
interface="$(ifconfig | grep -vm1 '^lo\|^ \|^$' | cut -f1 -d':')"
|
|
|
|
## AWK alternative
|
|
|
|
# ifconfig | awk -F: '!/^ / && !/^$/ && $1!="lo" {print $1;exit}'
|
2013-11-14 16:43:26 +01:00
|
|
|
fi
|
|
|
|
|
2014-11-25 01:23:36 +01:00
|
|
|
if [ ! -d "/sys/class/net/${interface}" ]; then
|
|
|
|
echo "Error: no such interface: ${interface}" >&2
|
|
|
|
else
|
|
|
|
path="/dev/shm/netspeed"
|
|
|
|
time=$(date +%s)
|
2013-11-14 16:43:26 +01:00
|
|
|
|
2014-11-25 01:23:36 +01:00
|
|
|
read rx < /sys/class/net/${interface}/statistics/rx_bytes
|
|
|
|
read tx < /sys/class/net/${interface}/statistics/tx_bytes
|
2013-11-14 16:43:26 +01:00
|
|
|
|
2014-11-25 01:23:36 +01:00
|
|
|
if [ ! -f "$path" ]; then
|
|
|
|
echo "$time $rx $tx" > "$path"
|
|
|
|
chmod 0666 "$path"
|
|
|
|
fi
|
2013-11-14 16:43:26 +01:00
|
|
|
|
2014-11-25 01:23:36 +01:00
|
|
|
read time_old rx_old tx_old < "$path"
|
|
|
|
echo "$time $rx $tx" > "$path"
|
|
|
|
time_diff=$(($time - $time_old))
|
2013-11-14 16:43:26 +01:00
|
|
|
|
2014-11-25 01:23:36 +01:00
|
|
|
if [ "$time_diff" -gt 0 ]; then
|
|
|
|
rx_rate=$((($rx - $rx_old) / $time_diff))
|
|
|
|
tx_rate=$((($tx - $tx_old) / $time_diff))
|
2013-11-14 16:43:26 +01:00
|
|
|
|
2014-11-25 01:23:36 +01:00
|
|
|
[ "$rx_rate" -gt 1024 ] && rx_rate=$(($rx_rate / 1024)) && rx_unit=K
|
|
|
|
[ "$rx_rate" -gt 1024 ] && rx_rate=$(($rx_rate / 1024)) && rx_unit=M
|
|
|
|
|
|
|
|
[ "$tx_rate" -gt 1024 ] && tx_rate=$(($tx_rate / 1024)) && tx_unit=K
|
|
|
|
[ "$tx_rate" -gt 1024 ] && tx_rate=$(($tx_rate / 1024)) && tx_unit=M
|
|
|
|
|
|
|
|
echo -n "$rx_rate $rx_unit↓ $tx_rate $tx_unit↑"
|
|
|
|
fi
|
|
|
|
fi
|