Below is a script that works on most embedded Linux routers such as Ubiquiti and OpenWRT compatible ones and gets its details from /proc/net/dev.
(And easy to change to packets etc.)
#!/bin/sh
SLP=1 # output / sleep interval
DEVICE=$1
IS_GOOD=0
for GOOD_DEVICE in `grep \: /proc/net/dev | awk -F: '{print $1}'`; do
if [ "$DEVICE" = $GOOD_DEVICE ]; then
IS_GOOD=1
break
fi
done
if [ $IS_GOOD -eq 0 ]; then
echo "Device not found. Should be one of these:"
grep ":" /proc/net/dev | awk -F: '{print $1}' | sed s@\ @@g
exit 1
fi
while true; do
LINE=`grep $1 /proc/net/dev | sed s/.*://`;
RECEIVED1=`echo $LINE | awk '{print $1}'`
TRANSMITTED1=`echo $LINE | awk '{print $9}'`
TOTAL=$(($RECEIVED1+$TRANSMITTED1))
sleep $SLP
LINE=`grep $1 /proc/net/dev | sed s/.*://`;
RECEIVED2=`echo $LINE | awk '{print $1}'`
TRANSMITTED2=`echo $LINE | awk '{print $9}'`
SPEED=$((($RECEIVED2+$TRANSMITTED2-$TOTAL)/$SLP))
INSPEED=$((($RECEIVED2-$RECEIVED1)/$SLP))
OUTSPEED=$((($TRANSMITTED2-$TRANSMITTED1)/$SLP))
printf "In: %12i KB/s | Out: %12i KB/s | Total: %12i KB/s\n" $(($INSPEED/1024)) $(($OUTSPEED/1024)) $((($INSPEED+$OUTSPEED)/1024)) ;
done;
Copy the above to your clipboard and then in a terminal session on your router:
$ cat > /tmp/n.sh
then: Ctrl+V (or right click / Paste)
then: Ctrl+D
$ chmod +x /tmp/n.sh
$ /tmp/n.sh eth0
You can also paste it in a notepad, then just repeat the above if you need to edit it - not all embedded routers have an editor! Make sure you copy everything from the # at the top to the done; at the bottom.
You might have to change the /bin/bash to /bin/sh or vice versa. There is another example that uses /sys here: How to get real-time network statistics in Linux with KB/MB/Bytes format and for specific port or application processID?
Source: https://gist.github.com/dagelf/ab2bad26ce96fa8d79b0834cd8cab549
netstat -i can be used as well, but same is necessary as CJSewell mentioned, you have to take samples with a time diff and calculate. – MaQleod – 2015-03-05T17:04:27.057