freedombone-wifi 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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 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 [ -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. if [ ! ${WIFI_SSID} ]; then
  185. echo $'No SSID given'
  186. exit 1
  187. fi
  188. if [[ ${WIFI_HOTSPOT} != 'no' ]]; then
  189. hotspot_on
  190. exit 0
  191. else
  192. hotspot_off
  193. fi
  194. if [[ "$WIFI_TYPE" != 'none' && "$WIFI_TYPE" != 'open' ]]; then
  195. if [ ! $WIFI_PASSPHRASE ]; then
  196. echo $'No wifi passphrase was given'
  197. exit 2
  198. fi
  199. fi
  200. if [[ ${WIFI_TYPE} == 'wpa2-psk' ]]; then
  201. if [ ! -d /etc/wpa_supplicant ]; then
  202. echo $'wpasupplicant package is not installed'
  203. exit 3
  204. fi
  205. wifi_wpa2_psk "$WIFI_SSID" "$WIFI_PASSPHRASE"
  206. exit 0
  207. fi
  208. if [[ "$WIFI_TYPE" == 'none' || "$WIFI_TYPE" == 'open' ]]; then
  209. wifi_none "$WIFI_SSID"
  210. exit 0
  211. fi
  212. exit 0