Skip to content

Instantly share code, notes, and snippets.

@cjsewell
Last active May 23, 2018 16:28
Show Gist options
  • Save cjsewell/a139be3c6db581da521d to your computer and use it in GitHub Desktop.
Save cjsewell/a139be3c6db581da521d to your computer and use it in GitHub Desktop.
netspeed.sh
#!/bin/bash
#shows speed on the specified device
###CHECKS####
DEVICE=$1
IS_GOOD=0
for GOOD_DEVICE in `grep ":" /proc/net/dev | awk '{print $1}' | sed s/:.*//`; do
if [ "$DEVICE" = "$GOOD_DEVICE" ]; then
IS_GOOD=1
break
fi
done
if [ $IS_GOOD -eq 0 ]; then
echo "Device $DEVICE not found. Should be one of these:"
grep ":" /proc/net/dev | awk '{print $1}' | sed s/:.*//
exit 1
fi
###REAL STUFF
LINE=`grep $1 /proc/net/dev | sed s/.*://`;
RECEIVED1=`echo $LINE | awk '{print $1}'`
TRANSMITTED1=`echo $LINE | awk '{print $9}'`
TOTAL=$(($RECEIVED1+$TRANSMITTED1))
SLP=3
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))
echo "In: $(($INSPEED/1024))KB/s | Out: $(($OUTSPEED/1024))KB/s";
@dagelf
Copy link

dagelf commented May 23, 2018

Added better spacing and continuous output: https://gist.github.com/dagelf/8a842ed755354b31e79214042a4a4695

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment