##########################################
# netbytes
# by invalidcode.com
#
# updated: 2008/10/08
#
# released under CC by-nc 2.5
# Attribution-Noncommercial 2.5 Generic
##########################################
if [ $# -ne 1 ]; then
echo "usage: netbytes [sample seconds]"
exit
fi
d=$1
if [ $d -le 0 ]; then
echo "[sample seconds] must be 1 or greater"
exit
fi
a=0
y=0
for i in `netstat -bni | grep -v "*" | grep -E "^en." | awk '{print $1}' | uniq`; do
iface=$i
t=`netstat -b -n -I ${iface} | grep ${iface} | tail -1 | awk '{print $7}'`
a=$(($a+t))
t=`netstat -b -n -I ${iface} | grep ${iface} | tail -1 | awk '{print $10}'`
y=$(($y+t))
done
sleep $d
b=0
z=0
for i in `netstat -bni | grep -v "*" | grep -E "^en." | awk '{print $1}' | uniq`; do
iface=$i
t=`netstat -b -n -I ${iface} | grep ${iface} | tail -1 | awk '{print $7}'`
b=$(($b+t))
t=`netstat -b -n -I ${iface} | grep ${iface} | tail -1 | awk '{print $10}'`
z=$(($z+t))
done
in=$((($b-$a)/1024/$d))
out=$((($z-$y)/1024/$d))
echo -n "in: ${in}kb/sec / out: ${out}kb/sec"