Skip to content

Instantly share code, notes, and snippets.

@mttjohnson
Forked from dagelf/n.sh
Last active February 25, 2020 10:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mttjohnson/3cadbdcca8832f60ed9546ad38af4b10 to your computer and use it in GitHub Desktop.
Save mttjohnson/3cadbdcca8832f60ed9546ad38af4b10 to your computer and use it in GitHub Desktop.
Linux network interface throughput speed
#!/bin/sh
# Forked from https://gist.github.com/dagelf/ab2bad26ce96fa8d79b0834cd8cab549
# With modifications for including timestamp and reporting in Megabits/s instead of Kilobytes/s
#
# To run: ./n.sh em1
SLP=60 # display / 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
CUR_TIME=$(date '+%Y-%m-%d %H:%M:%S')
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))
# Calculation in Megabits/s
printf "$CUR_TIME | In: %12i KB/s | Out: %12i KB/s | Total: %12i KB/s\n" $(($INSPEED/1024)) $(($OUTSPEED/1024)) $(($SPEED/1024)) ;
done;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment