nixos/home/hyprland/ironbar/reference/scripts/traffic.sh

51 lines
1.6 KiB
Bash
Raw Normal View History

2023-10-10 20:00:04 +01:00
#!/usr/bin/env bash
#shows traffic on the specified device
while :; do
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
LINE=$(grep $1 /proc/net/dev | sed s/.*://)
RECEIVED=$(echo $LINE | awk '{print $1}')
TRANSMITTED=$(echo $LINE | awk '{print $9}')
TOTAL=$(($RECEIVED + $TRANSMITTED))
SLP=1
sleep $SLP
LINE=$(grep $1 /proc/net/dev | sed s/.*://)
RECEIVED=$(echo $LINE | awk '{print $1}')
TRANSMITTED=$(echo $LINE | awk '{print $9}')
SPEED=$((($RECEIVED + $TRANSMITTED - $TOTAL) / $SLP))
echo "Current speed: $(human_readable $SPEED)/s"
done