freedombone-wifi 6.2KB

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