Skip to content

Instantly share code, notes, and snippets.

@andypotanin
Forked from radu-gheorghe/traffic.sh
Created June 27, 2020 08:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andypotanin/f3e39b60807347f531f985054c539c7d to your computer and use it in GitHub Desktop.
Save andypotanin/f3e39b60807347f531f985054c539c7d to your computer and use it in GitHub Desktop.
bash script for checking out traffic via /proc/net/dev
#!/bin/bash
#shows traffic on the specified device
function human_readable {
VALUE=$1
BIGGIFIERS=( B K M G )
CURRENT_BIGGIFIER=0
while [ $VALUE -gt 10000 ] ;do
VALUE=$(($VALUE/1000))
CURRENT_BIGGIFIER=$((CURRENT_BIGGIFIER+1))
done
#echo "value: $VALUE"
#echo "biggifier: ${BIGGIFIERS[$CURRENT_BIGGIFIER]}"
echo "$VALUE${BIGGIFIERS[$CURRENT_BIGGIFIER]}"
}
###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
RECEIVED=`grep $1 /proc/net/dev | awk '{print $2}'`
TRANSMITTED=`grep $1 /proc/net/dev | awk '{print $10}'`
TOTAL=$(($RECEIVED+$TRANSMITTED))
echo "Transmitted: `human_readable $TRANSMITTED`"
echo "Received: `human_readable $RECEIVED`"
echo "Total: `human_readable $TOTAL`"
SLP=3
echo "Sleeping $SLP to calculate speed..."
sleep $SLP
RECEIVED=`grep $1 /proc/net/dev | awk '{print $2}'`
TRANSMITTED=`grep $1 /proc/net/dev | awk '{print $10}'`
SPEED=$((($RECEIVED+$TRANSMITTED-$TOTAL)/$SLP))
echo "Current speed: `human_readable $SPEED`/s"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment