Advertisement
Guest User

Untitled

a guest
Nov 24th, 2019
301
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.76 KB | None | 0 0
  1. #!/bin/sh
  2.  
  3. # Authors:
  4. # - Moritz Warning <moritzwarning@web.de> (2016)
  5. # - Zhong Jianxin <azuwis@gmail.com> (2014)
  6. #
  7. # See file LICENSE at the project root directory for license information.
  8. #
  9. # i3status.conf should contain:
  10. # general {
  11. # output_format = i3bar
  12. # }
  13. #
  14. # i3 config looks like this:
  15. # bar {
  16. # status_command exec /usr/share/doc/i3status/contrib/net-speed.sh
  17. # }
  18. #
  19. # Single interface:
  20. # ifaces="eth0"
  21. #
  22. # Multiple interfaces:
  23. # ifaces="eth0 wlan0"
  24. #
  25.  
  26. # Auto detect interfaces
  27. ifaces=$(ls /sys/class/net | grep -E '^(eth|wlan|enp|wlp)')
  28.  
  29. last_time=0
  30. last_rx=0
  31. last_tx=0
  32. rate=""
  33.  
  34. readable() {
  35. local bytes=$1
  36. local kib=$(( bytes >> 10 ))
  37. if [ $kib -lt 0 ]; then
  38. echo "? K"
  39. elif [ $kib -gt 1024 ]; then
  40. local mib_int=$(( kib >> 10 ))
  41. local mib_dec=$(( kib % 1024 * 976 / 10000 ))
  42. if [ "$mib_dec" -lt 10 ]; then
  43. mib_dec="0${mib_dec}"
  44. fi
  45. echo "${mib_int}.${mib_dec} M"
  46. else
  47. echo "${kib} K"
  48. fi
  49. }
  50.  
  51. update_rate() {
  52. local time=$(date +%s)
  53. local rx=0 tx=0 tmp_rx tmp_tx
  54.  
  55. for iface in $ifaces; do
  56. read tmp_rx < "/sys/class/net/${iface}/statistics/rx_bytes"
  57. read tmp_tx < "/sys/class/net/${iface}/statistics/tx_bytes"
  58. rx=$(( rx + tmp_rx ))
  59. tx=$(( tx + tmp_tx ))
  60. done
  61.  
  62. local interval=$(( $time - $last_time ))
  63. if [ $interval -gt 0 ]; then
  64. rate="$(readable $(( (rx - last_rx) / interval )))↓ $(readable $(( (tx - last_tx) / interval )))↑"
  65. else
  66. rate=""
  67. fi
  68.  
  69. last_time=$time
  70. last_rx=$rx
  71. last_tx=$tx
  72. }
  73.  
  74. i3status | (read line && echo "$line" && read line && echo "$line" && read line && echo "$line" && update_rate && while :
  75. do
  76. read line
  77. update_rate
  78. echo ",[{\"full_text\":\"${rate}\" },${line#,\[}" || exit 1
  79. done)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement