Skip to content

Instantly share code, notes, and snippets.

@wingrime
Created November 21, 2018 09:26
Show Gist options
  • Save wingrime/4698a58742c79fcaea12860fd4248e20 to your computer and use it in GitHub Desktop.
Save wingrime/4698a58742c79fcaea12860fd4248e20 to your computer and use it in GitHub Desktop.
linux trafic monitor
function humanValue()
{
h=( '' K M G T P )
i=1; v=$(( $1 * 8 ))
while [ $v -gt $(( 1 << 10 * i )) ]; do let i++; done;
echo -n "$(( $v >> 10 * --i )) ${h[i]}b/s";
}
ifaces=$(ip addr | grep -E "^[0-9]:" | cut -d" " -f2 | tr -d \:)
declare -A RX2 TX2;
while sleep 1;
do
date
for INTERFACE in $ifaces;
do
RX1=$(cat /sys/class/net/${INTERFACE}/statistics/rx_bytes)
TX1=$(cat /sys/class/net/${INTERFACE}/statistics/tx_bytes)
DOWN=$(( RX1 - RX2[$INTERFACE] ))
UP=$(( TX1 - TX2[$INTERFACE] ))
RX2[$INTERFACE]=$RX1; TX2[$INTERFACE]=$TX1
echo -e "[ $INTERFACE:\tRX: $(humanValue $DOWN)\t|\tTX: $(humanValue $UP) ]"
done;
done;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment