wifi_quality 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. #!/bin/sh -e
  2. #
  3. # wifi_quality: display wifi signal quality
  4. #
  5. # Copyright (C) 2008 Canonical Ltd.
  6. # Copyright (C) 2011-2014 Dustin Kirkland
  7. #
  8. # Authors: Dustin Kirkland <kirkland@byobu.org>
  9. #
  10. # This program is free software: you can redistribute it and/or modify
  11. # it under the terms of the GNU General Public License as published by
  12. # the Free Software Foundation, version 3 of the License.
  13. #
  14. # This program is distributed in the hope that it will be useful,
  15. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. # GNU General Public License for more details.
  18. #
  19. # You should have received a copy of the GNU General Public License
  20. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. ___get_dev_list() {
  22. if [ -n "$MONITORED_NETWORK" ]; then
  23. echo "$MONITORED_NETWORK"
  24. else
  25. iw dev | grep Interface | cut -f2 -d\
  26. fi
  27. }
  28. __wifi_quality_detail() {
  29. if eval $BYOBU_TEST iw >/dev/null 2>&1; then
  30. local dev
  31. for dev in $(___get_dev_list); do
  32. iw dev "$dev" info
  33. iw dev "$dev" link
  34. echo
  35. done
  36. elif eval $BYOBU_TEST iwconfig >/dev/null 2>&1; then
  37. iwconfig 2>/dev/null
  38. fi
  39. }
  40. __wifi_quality() {
  41. local out bitrate quality
  42. if eval $BYOBU_TEST iwconfig >/dev/null 2>&1; then
  43. # iwconfig is expected to output lines like:
  44. # Bit Rate=54 Mb/s Tx-Power=15 dBm
  45. # Link Quality=60/70 Signal level=-50 dBm
  46. # the awk below tokenizes the output and prints shell evalable results
  47. out=`iwconfig $MONITORED_NETWORK 2>/dev/null |
  48. awk '$0 ~ /[ ]*Link Quality./ {
  49. sub(/.*=/,"",$2); split($2,a,"/");
  50. printf "quality=%.0f\n", 100*a[1]/a[2] };
  51. $0 ~ /[ ]*Bit Rate/ { sub(/.*[:=]/,"",$2); printf("bitrate=%s\n", $2); }
  52. '`
  53. eval "$out"
  54. elif eval $BYOBU_TEST iw >/dev/null 2>&1; then
  55. local dev
  56. for dev in $(___get_dev_list); do
  57. # iw is expected to output lines like:
  58. # signal: -50 dBm
  59. # rx bitrate: 216.0 MBit/s MCS 13 40MHz
  60. # signal to quality: https://superuser.com/a/1360447
  61. out=`iw dev "$dev" link 2>/dev/null |
  62. awk '$0 ~ /^\s*signal:/ { a = 100 * ($2 + 110) / 70;
  63. printf "quality=%.0f\n", (a > 100) ? 100 : ((a < 0) ? 0 : a); }
  64. $0 ~ /^\s*rx bitrate:/ { printf "bitrate=%s\n", $3; }
  65. '`
  66. eval "$out"
  67. [ -z "$bitrate" ] || [ -z "$quality" ] || break
  68. done
  69. fi
  70. [ -n "$bitrate" ] || bitrate=0
  71. [ -n "$quality" ] || quality=0
  72. if [ "$bitrate" -gt 0 ] && [ "$quality" -gt 0 ]; then
  73. printf "${ICON_WIFI}"; color b C k; printf "%s" "$bitrate"; color -; color C k; printf "%s" "$ICON_MBPS"; color -; color b C k; printf "%s" "$quality"; color -; color C k; printf "%s" "$PCT"; color --
  74. else
  75. rm -f "$BYOBU_RUN_DIR/status.$BYOBU_BACKEND/wifi_quality"*
  76. fi
  77. }
  78. # vi: syntax=sh ts=4 noexpandtab