You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

114 lines
2.6 KiB

  1. #!/bin/sh
  2. . /lib/functions.sh
  3. log() {
  4. local msg="$1"
  5. logger -t airos-dfs-reset -s "$msg"
  6. }
  7. rexec() {
  8. local target="$1"
  9. local username="$2"
  10. local password="$3"
  11. local cmd="$4"
  12. raw=$(DROPBEAR_PASSWORD="$password" ssh -y $username@$target "$cmd")
  13. ssh_result=$?
  14. }
  15. reset_dfs() {
  16. local cmd="/usr/etc/rc.d/rc.softrestart force"
  17. rexec $* "$cmd"
  18. }
  19. get_running_freq() {
  20. local cmd="iwconfig ath0 | grep Frequency | awk -F ':' '{print \$3}' | awk '{print \$1}' | sed 's/\.//'"
  21. rexec $* "$cmd"
  22. # Append zeroes which are then cut to 4, we have to convert GHz into MHz
  23. raw="$raw"000
  24. running_freq=${raw:0:4}
  25. }
  26. get_target_freq() {
  27. local cmd="grep 'radio.1.freq' /tmp/system.cfg | awk -F '=' '{ print \$2}'"
  28. rexec $* "$cmd"
  29. target_freq="$raw"
  30. }
  31. check_dfs() {
  32. local target="$1"
  33. local username="$2"
  34. local password="$3"
  35. get_running_freq $target $username $password
  36. if [ "$ssh_result" != 0 ]; then
  37. return
  38. fi
  39. get_target_freq $target $username $password
  40. if [ "$ssh_result" != 0 ]; then
  41. return
  42. fi
  43. log "Running freq: $running_freq - Target freq: $target_freq"
  44. [ "$running_freq" == "$target_freq" ]
  45. }
  46. reset_allowed() {
  47. local daytime_limit="$1"
  48. local start="$(echo $daytime_limit | awk -F '-' '{print $1'})"
  49. local end="$(echo $daytime_limit | awk -F '-' '{print $2'})"
  50. local cur="$(date +%H)"
  51. [ "$cur" -ge "$start" ] && [ "$cur" -le "$end" ]
  52. }
  53. handle_device() {
  54. local device="$1"
  55. config_get target "$device" target
  56. config_get username "$device" username
  57. config_get password "$device" password
  58. config_get daytime_limit "$device" daytime_limit "0-23"
  59. ssh_result=0
  60. log "Checking Device $device"
  61. check_dfs $target $username $password
  62. freqmatch=$?
  63. if [ "$ssh_result" != 0 ]; then
  64. log "ssh exited non-zero - connect timeout?"
  65. return
  66. elif [ "$freqmatch" == 0 ]; then
  67. log "Frequency is matching. No radar event fired"
  68. else
  69. log "Frequency doesnt match. Looks like DFS activity :("
  70. if reset_allowed $daytime_limit; then
  71. log "Initiating reset"
  72. reset_dfs $target $username $password
  73. log "Waiting $cfg_reset_sleep seconds after reset"
  74. sleep $cfg_reset_sleep
  75. else
  76. log "Resetting is forbidden at this daytime"
  77. fi
  78. fi
  79. }
  80. main() {
  81. log "started!"
  82. config_load airos-dfs-reset
  83. config_get cfg_interval general interval 600
  84. config_get cfg_reset_sleep general reset_sleep 120
  85. while :;
  86. do
  87. config_foreach handle_device device
  88. sleep $cfg_interval
  89. done
  90. }
  91. main