net_use_check.sh 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. #!/bin/bash
  2. ## Forked from https://github.com/polybar/polybar-scripts/blob/master/polybar-scripts/network-traffic/network-traffic.sh
  3. print_bytes() {
  4. if [ "$1" -eq 0 ] || [ "$1" -lt 1000 ]; then
  5. bytes=" 0"
  6. elif [ "$1" -lt 1000000 ]; then
  7. bytes="$(echo "scale=0;$1/1000" | bc -l)k"
  8. else
  9. bytes="$(echo "scale=1;$1/1000000" | bc -l)%{F${M_HI}}M"
  10. fi
  11. echo "$bytes"
  12. }
  13. ## Variables
  14. INTERVAL=2
  15. ## Space delimited list of interfaces: INTERFACES="eth0 wlan0"
  16. INTERFACES="wlp0s20f3"
  17. declare -A bytes
  18. ## Declare colors
  19. M_FG="#C5C8C6"
  20. M_FG_ICON="#979997"
  21. M_HI="#F0C674" ## byellow
  22. for interface in ${INTERFACES}; do
  23. bytes[past_rx_${interface}]="$(cat /sys/class/net/"${interface}"/statistics/rx_bytes)"
  24. bytes[past_tx_${interface}]="$(cat /sys/class/net/"${interface}"/statistics/tx_bytes)"
  25. done
  26. while true; do
  27. down=0
  28. up=0
  29. for interface in ${INTERFACES}; do
  30. bytes[now_rx_${interface}]="$(cat /sys/class/net/"${interface}"/statistics/rx_bytes)"
  31. bytes[now_tx_${interface}]="$(cat /sys/class/net/"${interface}"/statistics/tx_bytes)"
  32. bytes_down=$((((${bytes[now_rx_${interface}]} - ${bytes[past_rx_${interface}]})) / INTERVAL))
  33. bytes_up=$((((${bytes[now_tx_${interface}]} - ${bytes[past_tx_${interface}]})) / INTERVAL))
  34. down=$(((( "${down}" + "${bytes_down}" ))))
  35. up=$(((( "${up}" + "${bytes_up}" ))))
  36. bytes[past_rx_${interface}]=${bytes[now_rx_${interface}]}
  37. bytes[past_tx_${interface}]=${bytes[now_tx_${interface}]}
  38. done
  39. ## Return results
  40. echo "%{F${M_FG_ICON}}%{F${M_FG}}$(print_bytes ${down})%{F${M_FG_ICON}}%{F${M_FG}}$(print_bytes ${up})"
  41. ## Delay next run
  42. sleep ${INTERVAL}
  43. done