Skip to content

Instantly share code, notes, and snippets.

@maesoser
Last active August 12, 2019 09:13
Show Gist options
  • Select an option

  • Save maesoser/b82dc74117057b36d440064363e98ca5 to your computer and use it in GitHub Desktop.

Select an option

Save maesoser/b82dc74117057b36d440064363e98ca5 to your computer and use it in GitHub Desktop.
Net interfaces throughput top written in bash
#!/usr/bin/env bash
INTERVAL=5
NETPATH="/sys/class/net"
RX1_ARR=()
TX1_ARR=()
RX2_ARR=()
TX2_ARR=()
while true; do
NAME_ARR=()
for interface in $NETPATH/*; do
interface=$(echo $interface | rev | cut -d/ -f1 | rev)
NAME_ARR+=($interface)
done
RX_TOTAL=0
TX_TOTAL=0
clear && printf "\e[7m%-16s\t%-9s\t%-9s\n\e[0m" "Iface Name" "RX Kbps" "TX Kbps"
for i in "${!NAME_ARR[@]}"; do
RX2_ARR[$i]=${RX1_ARR[$i]}
TX2_ARR[$i]=${TX1_ARR[$i]}
RX1_ARR[$i]=`cat $NETPATH/${NAME_ARR[$i]}/statistics/rx_bytes`
TX1_ARR[$i]=`cat $NETPATH/${NAME_ARR[$i]}/statistics/tx_bytes`
RX_KBPS=$[(${RX2_ARR[$i]} - ${RX1_ARR[$i]}) / -1024 / $INTERVAL]
TX_KBPS=$[(${TX2_ARR[$i]} - ${TX1_ARR[$i]}) / -1024 / $INTERVAL]
TX_TOTAL=$[$TX_TOTAL + $TX_KBPS]
RX_TOTAL=$[$RX_TOTAL + $RX_KBPS]
if [ "$RX_KBPS" != "0" ] || [ "$TX_KBPS" != "0" ]; then
printf "%-16s\t%9d\t%9d\n" "${NAME_ARR[$i]}" "$RX_KBPS" "$TX_KBPS"
fi
done
printf "\n\e[7mTOTAL\tRX: %9d\tTX: %9d\e[0m" "$RX_TOTAL" "$TX_TOTAL"
sleep "$((INTERVAL))s"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment