Alexinfinito

bandwidth.sh

Jul 20th, 2016
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.60 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. TYPE=$1
  4. if [ "$1" != "rx" -a "$1" != "tx" ]; then
  5. echo "Invalid argument: $1" 1>&2
  6. echo "Must be 'tx' or 'rx'" 1>&2
  7. echo "Usage: $0 (rx|tx)" 1>&2
  8. exit 1
  9. fi
  10.  
  11. FILE=/tmp/i3blocks_bandwidth_$TYPE
  12. touch $FILE
  13.  
  14. PREV=$(cat $FILE)
  15. CUR=0
  16.  
  17. NETDIR=/sys/class/net
  18. for IFACE in $(ls -1 $NETDIR); do
  19. # Skip the loopback interface
  20. if [ "$IFACE" == "lo" ]; then
  21. continue
  22. fi
  23. F=$NETDIR/$IFACE/statistics/${TYPE}_bytes
  24. N=$(cat $F)
  25. CUR=$(expr $CUR + $N)
  26. done
  27.  
  28. delta=$(expr $CUR - $PREV)
  29. echo $(expr $delta / 1000) kB/s
  30.  
  31. echo $CUR > $FILE
Advertisement
Add Comment
Please, Sign In to add comment