freedombone-wifi 6.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. #!/bin/bash
  2. # _____ _ _
  3. # | __|___ ___ ___ _| |___ _____| |_ ___ ___ ___
  4. # | __| _| -_| -_| . | . | | . | . | | -_|
  5. # |__| |_| |___|___|___|___|_|_|_|___|___|_|_|___|
  6. #
  7. # Freedom in the Cloud
  8. #
  9. # Wifi configuration tools
  10. #
  11. # License
  12. # =======
  13. #
  14. # Copyright (C) 2016-2018 Bob Mottram <bob@freedombone.net>
  15. #
  16. # This program is free software: you can redistribute it and/or modify
  17. # it under the terms of the GNU Affero General Public License as published by
  18. # the Free Software Foundation, either version 3 of the License, or
  19. # (at your option) any later version.
  20. #
  21. # This program is distributed in the hope that it will be useful,
  22. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24. # GNU Affero General Public License for more details.
  25. #
  26. # You should have received a copy of the GNU Affero General Public License
  27. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  28. PROJECT_NAME='freedombone'
  29. export TEXTDOMAIN=${PROJECT_NAME}-wifi
  30. export TEXTDOMAINDIR="/usr/share/locale"
  31. CONFIGURATION_FILE=$HOME/${PROJECT_NAME}.cfg
  32. COMPLETION_FILE=$HOME/${PROJECT_NAME}-completed.txt
  33. WIFI_INTERFACE=wlan0
  34. wifi_interface_specified=
  35. WIFI_TYPE='wpa2-psk'
  36. WIFI_SSID=
  37. WIFI_PASSPHRASE=
  38. WIFI_HOTSPOT='no'
  39. WIFI_CONFIG=/etc/wpa_supplicant/wpa_supplicant.conf
  40. WIFI_NETWORKS_FILE=~/${PROJECT_NAME}-wifi.cfg
  41. NETWORKS_INTERACTIVE=
  42. WIFI_DISABLE=
  43. WAIT_SEC=
  44. WIFI_MAX_RETRIES=5
  45. IFACE=
  46. IFACE_SECONDARY=
  47. source /usr/share/${PROJECT_NAME}/utils/${PROJECT_NAME}-utils-config
  48. source /usr/share/${PROJECT_NAME}/utils/${PROJECT_NAME}-utils-wifi
  49. function show_help {
  50. echo ''
  51. echo $"${PROJECT_NAME}-wifi -i [interface] -t [type] -s [ssid] -p [passphrase]"
  52. echo ''
  53. echo $'Wifi configuration tool'
  54. echo ''
  55. echo $' --help Show help'
  56. echo $' -i --interface [wlan0|wlan1...] Device name'
  57. echo $' -t --type [wpa2-psk|none|open] Security type'
  58. echo $' -s --ssid [id] Set SSID'
  59. echo $' -p --passphrase [text] Set passphrase'
  60. echo $' --hotspot [yes|no] Create a hotspot'
  61. echo $' --networks [filename] File containing wifi networks'
  62. echo $' --createnetworks [filename] Create file containing wifi networks'
  63. echo $' --disable [yes/no] Disable wifi'
  64. echo $' --retries [number] Maximum number of retries'
  65. echo ''
  66. exit 0
  67. }
  68. while [ $# -gt 1 ]
  69. do
  70. key="$1"
  71. case $key in
  72. --help)
  73. show_help
  74. ;;
  75. -i|--if|--interface)
  76. shift
  77. WIFI_INTERFACE=${1}
  78. wifi_interface_specified=1
  79. write_config_param "WIFI_INTERFACE" "$WIFI_INTERFACE"
  80. ;;
  81. --wait|--sleep|--pause)
  82. shift
  83. WAIT_SEC=${1}
  84. ;;
  85. -t|--type)
  86. shift
  87. WIFI_TYPE=${1}
  88. ;;
  89. -s|--ssid)
  90. shift
  91. WIFI_SSID=${1}
  92. ;;
  93. --retries)
  94. shift
  95. WIFI_MAX_RETRIES=${1}
  96. ;;
  97. -p|--pass|--passphrase)
  98. shift
  99. WIFI_PASSPHRASE=${1}
  100. ;;
  101. --hotspot)
  102. shift
  103. WIFI_HOTSPOT=${1}
  104. ;;
  105. --networks)
  106. shift
  107. WIFI_NETWORKS_FILE=${1}
  108. ;;
  109. --networksinteractive)
  110. shift
  111. NETWORKS_INTERACTIVE='yes'
  112. WIFI_NETWORKS_FILE=${1}
  113. ;;
  114. --disable)
  115. shift
  116. WIFI_DISABLE=${1}
  117. if [[ $WIFI_DISABLE == $'yes' || $WIFI_DISABLE == $'y' ]]; then
  118. WIFI_DISABLE='yes'
  119. else
  120. WIFI_DISABLE='no'
  121. fi
  122. ;;
  123. *)
  124. # unknown option
  125. ;;
  126. esac
  127. shift
  128. done
  129. if [ ${NETWORKS_INTERACTIVE} ]; then
  130. create_networks_interactive
  131. exit 0
  132. fi
  133. if [ ! ${wifi_interface_specified} ]; then
  134. if [ ! "$WAIT_SEC" ]; then
  135. wpa_action "${WIFI_INTERFACE}" stop
  136. wpa_cli -i "${WIFI_INTERFACE}" terminate
  137. else
  138. sleep "${WAIT_SEC}"
  139. fi
  140. update_wifi_adaptors 'wlan'
  141. if [ ! $IFACE ]; then
  142. echo $'No wifi adaptors were found'
  143. exit 872356
  144. fi
  145. WIFI_INTERFACE=${IFACE}
  146. echo "Adaptor: $WIFI_INTERFACE"
  147. write_config_param "WIFI_INTERFACE" "$WIFI_INTERFACE"
  148. fi
  149. if [ ${WIFI_DISABLE} ]; then
  150. disable_wifi ${WIFI_DISABLE}
  151. exit 0
  152. fi
  153. if [[ ${WIFI_HOTSPOT} == 'no' ]]; then
  154. if [ -f "${WIFI_NETWORKS_FILE}" ]; then
  155. wifi_established=
  156. wifi_retry_ctr=0
  157. while [ ! $wifi_established ]; do
  158. if [ ${wifi_retry_ctr} -gt 0 ]; then
  159. wpa_action ${WIFI_INTERFACE} stop
  160. wpa_cli -i ${WIFI_INTERFACE} terminate
  161. fi
  162. networks_from_file
  163. # allow some time for a connection to be established
  164. sleep 5
  165. # has it worked?
  166. if [[ $(wifi_is_running) != "0" ]]; then
  167. wifi_established=1
  168. break
  169. fi
  170. # has the limit of retries been reached?
  171. wifi_retry_ctr=$((wifi_retry_ctr+1))
  172. if [ ${wifi_retry_ctr} -ge "${WIFI_MAX_RETRIES}" ]; then
  173. break
  174. fi
  175. done
  176. if [ $wifi_established ]; then
  177. wpa_cli status
  178. exit 0
  179. else
  180. echo $'Wifi could not be started'
  181. exit 4
  182. fi
  183. fi
  184. fi
  185. if [ ! "${WIFI_SSID}" ]; then
  186. echo $'No SSID given'
  187. exit 1
  188. fi
  189. if [[ ${WIFI_HOTSPOT} != 'no' ]]; then
  190. if ! hotspot_on; then
  191. exit "$?"
  192. fi
  193. exit 0
  194. else
  195. hotspot_off
  196. fi
  197. if [[ "$WIFI_TYPE" != 'none' && "$WIFI_TYPE" != 'open' ]]; then
  198. if [ ! "$WIFI_PASSPHRASE" ]; then
  199. echo $'No wifi passphrase was given'
  200. exit 2
  201. fi
  202. fi
  203. if [[ ${WIFI_TYPE} == 'wpa2-psk' ]]; then
  204. if [ ! -d /etc/wpa_supplicant ]; then
  205. echo $'wpasupplicant package is not installed'
  206. exit 3
  207. fi
  208. wifi_wpa2_psk "$WIFI_SSID" "$WIFI_PASSPHRASE"
  209. exit 0
  210. fi
  211. if [[ "$WIFI_TYPE" == 'none' || "$WIFI_TYPE" == 'open' ]]; then
  212. wifi_none "$WIFI_SSID"
  213. exit 0
  214. fi
  215. exit 0