Guest User

Untitled

a guest
Nov 24th, 2019
1,641
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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="enp3s0"
  21. #
  22. # Multiple interfaces:
  23. # ifaces="enp3s0 wlp2s0b1"
  24. #
  25.  
  26. # Auto detect interfaces
  27. ifaces=$(ls /sys/class/net | grep -E '^(enp3s0|wlp2s0b1)')
  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 )))↓"
  65. rate2="$(readable $(( (tx - last_tx) / interval )))↑"
  66. else
  67. rate=""
  68. fi
  69.  
  70. last_time=$time
  71. last_rx=$rx
  72. last_tx=$tx
  73. }
  74.  
  75. i3status | (read line && echo "$line" && read line && echo "$line" && read line && echo "$line" && update_rate && while :
  76. do
  77. read line
  78. update_rate
  79. echo ",[{\"full_text\":\"${rate}\",\"color\":\"#00cc99\" },{\"full_text\":\"${rate2}\",\"color\":\"#ffac18\" },${line#,\[}" || exit 1
  80. done)
RAW Paste Data