1
0

travelmate.sh 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. #!/bin/sh
  2. # travelmate, a wlan connection manager for travel router
  3. # written by Dirk Brenken (dev@brenken.org)
  4. # This is free software, licensed under the GNU General Public License v3.
  5. # You should have received a copy of the GNU General Public License
  6. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  7. # prepare environment
  8. #
  9. LC_ALL=C
  10. PATH="/usr/sbin:/usr/bin:/sbin:/bin"
  11. trm_ver="0.3.2"
  12. trm_enabled=1
  13. trm_debug=0
  14. trm_maxwait=20
  15. trm_maxretry=3
  16. trm_iw=1
  17. f_envload()
  18. {
  19. # source required system libraries
  20. #
  21. if [ -r "/lib/functions.sh" ]
  22. then
  23. . "/lib/functions.sh"
  24. else
  25. f_log "error" "status ::: required system library not found"
  26. fi
  27. # load uci config and check 'enabled' option
  28. #
  29. option_cb()
  30. {
  31. local option="${1}"
  32. local value="${2}"
  33. eval "${option}=\"${value}\""
  34. }
  35. config_load travelmate
  36. if [ ${trm_enabled} -ne 1 ]
  37. then
  38. f_log "info " "status ::: travelmate is currently disabled, please set 'trm_enabled' to '1' to use this service"
  39. exit 0
  40. fi
  41. # check for preferred wireless tool
  42. #
  43. if [ ${trm_iw} -eq 1 ]
  44. then
  45. trm_scanner="$(which iw)"
  46. else
  47. trm_scanner="$(which iwinfo)"
  48. fi
  49. if [ -z "${trm_scanner}" ]
  50. then
  51. f_log "error" "status ::: no wireless tool for wlan scanning found, please install 'iw' or 'iwinfo'"
  52. fi
  53. }
  54. # function to bring down all STA interfaces
  55. #
  56. f_prepare()
  57. {
  58. local config="${1}"
  59. local mode="$(uci -q get wireless."${config}".mode)"
  60. local network="$(uci -q get wireless."${config}".network)"
  61. local disabled="$(uci -q get wireless."${config}".disabled)"
  62. if [ "${mode}" = "sta" ] && [ -n "${network}" ]
  63. then
  64. trm_stalist="${trm_stalist} ${config}_${network}"
  65. if [ -z "${disabled}" ] || [ "${disabled}" = "0" ]
  66. then
  67. uci -q set wireless."${config}".disabled=1
  68. f_log "debug" "prepare ::: config: ${config}, interface: ${network}"
  69. fi
  70. fi
  71. }
  72. f_check()
  73. {
  74. local ifname cnt=1 mode="${1}"
  75. trm_ifstatus="false"
  76. while [ ${cnt} -le ${trm_maxwait} ]
  77. do
  78. if [ "${mode}" = "ap" ]
  79. then
  80. trm_ifstatus="$(ubus -S call network.wireless status | jsonfilter -l1 -e '@.*.up')"
  81. else
  82. ifname="$(ubus -S call network.wireless status | jsonfilter -l1 -e '@.*.interfaces[@.config.mode="sta"].ifname')"
  83. if [ -n "${ifname}" ]
  84. then
  85. trm_ifstatus="$(ubus -S call network.interface dump | jsonfilter -e "@.interface[@.device=\"${ifname}\"].up")"
  86. fi
  87. fi
  88. if [ "${mode}" = "initial" ] || [ "${trm_ifstatus}" = "true" ]
  89. then
  90. break
  91. fi
  92. cnt=$((cnt+1))
  93. sleep 1
  94. done
  95. f_log "debug" "check ::: mode: ${mode}, name: ${ifname}, status: ${trm_ifstatus}, count: ${cnt}, max-wait: ${trm_maxwait}"
  96. }
  97. # function to write to syslog
  98. #
  99. f_log()
  100. {
  101. local class="${1}"
  102. local log_msg="${2}"
  103. if [ -n "${log_msg}" ] && ([ "${class}" != "debug" ] || [ ${trm_debug} -eq 1 ])
  104. then
  105. logger -t "travelmate-[${trm_ver}] ${class}" "${log_msg}"
  106. if [ "${class}" = "error" ]
  107. then
  108. exit 255
  109. fi
  110. fi
  111. }
  112. f_main()
  113. {
  114. local ap_list ssid_list config network ssid cnt=1
  115. local sysver="$(ubus -S call system board | jsonfilter -e '@.release.description')"
  116. f_check "initial"
  117. if [ "${trm_ifstatus}" != "true" ]
  118. then
  119. config_load wireless
  120. config_foreach f_prepare wifi-iface
  121. if [ -n "$(uci -q changes wireless)" ]
  122. then
  123. uci -q commit wireless
  124. ubus call network reload
  125. fi
  126. f_check "ap"
  127. ap_list="$(ubus -S call network.wireless status | jsonfilter -e '@.*.interfaces[@.config.mode="ap"].ifname')"
  128. f_log "debug" "main ::: ap-list: ${ap_list}, sta-list: ${trm_stalist}"
  129. if [ -z "${ap_list}" ] || [ -z "${trm_stalist}" ]
  130. then
  131. f_log "error" "status ::: no usable AP/STA configuration found"
  132. fi
  133. for ap in ${ap_list}
  134. do
  135. while [ ${cnt} -le ${trm_maxretry} ]
  136. do
  137. if [ ${trm_iw} -eq 1 ]
  138. then
  139. ssid_list="$(${trm_scanner} dev "${ap}" scan 2>/dev/null | \
  140. awk '/SSID: /{if(!seen[$0]++){printf "\"";for(i=2; i<=NF; i++)if(i==2)printf $i;else printf " "$i;printf "\" "}}')"
  141. else
  142. ssid_list="$(${trm_scanner} "${ap}" scan | \
  143. awk '/ESSID: ".*"/{ORS=" ";if (!seen[$0]++) for(i=2; i<=NF; i++) print $i}')"
  144. fi
  145. f_log "debug" "main ::: scan-tool: ${trm_scanner}, ssidlist: ${ssid_list}"
  146. if [ -n "${ssid_list}" ]
  147. then
  148. for sta in ${trm_stalist}
  149. do
  150. config="${sta%%_*}"
  151. network="${sta##*_}"
  152. ssid="\"$(uci -q get wireless."${config}".ssid)\""
  153. if [ -n "$(printf "${ssid_list}" | grep -Fo "${ssid}")" ]
  154. then
  155. uci -q set wireless."${config}".disabled=0
  156. uci -q commit wireless
  157. ubus call network reload
  158. f_check "sta"
  159. if [ "${trm_ifstatus}" = "true" ]
  160. then
  161. f_log "info " "status ::: wwan interface connected to uplink ${ssid} (${cnt}/${trm_maxretry}, ${sysver})"
  162. sleep 5
  163. return 0
  164. else
  165. uci -q set wireless."${config}".disabled=1
  166. uci -q commit wireless
  167. ubus call network reload
  168. f_log "info " "status ::: wwan interface can't connect to uplink ${ssid} (${cnt}/${trm_maxretry}, ${sysver})"
  169. fi
  170. fi
  171. done
  172. else
  173. f_log "info " "status ::: empty uplink list (${cnt}/${trm_maxretry}, ${sysver})"
  174. fi
  175. cnt=$((cnt+1))
  176. sleep 5
  177. done
  178. done
  179. f_log "info " "status ::: no wwan uplink found (${sysver})"
  180. else
  181. f_log "info " "status ::: wwan uplink still connected (${sysver})"
  182. fi
  183. }
  184. if [ "${trm_procd}" = "true" ]
  185. then
  186. f_envload
  187. f_main
  188. fi
  189. exit 0