freedombone-wifi 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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@robotics.uk.to>
  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. 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 ''
  65. exit 0
  66. }
  67. while [[ $# > 1 ]]
  68. do
  69. key="$1"
  70. case $key in
  71. --help)
  72. show_help
  73. ;;
  74. -i|--if|--interface)
  75. shift
  76. WIFI_INTERFACE=${1}
  77. wifi_interface_specified=1
  78. write_config_param "WIFI_INTERFACE" "$WIFI_INTERFACE"
  79. ;;
  80. --wait|--sleep|--pause)
  81. shift
  82. WAIT_SEC=${1}
  83. ;;
  84. -t|--type)
  85. shift
  86. WIFI_TYPE=${1}
  87. ;;
  88. -s|--ssid)
  89. shift
  90. WIFI_SSID=${1}
  91. ;;
  92. -p|--pass|--passphrase)
  93. shift
  94. WIFI_PASSPHRASE=${1}
  95. ;;
  96. --hotspot)
  97. shift
  98. WIFI_HOTSPOT=${1}
  99. ;;
  100. --networks)
  101. shift
  102. WIFI_NETWORKS_FILE=${1}
  103. ;;
  104. --networksinteractive)
  105. shift
  106. NETWORKS_INTERACTIVE='yes'
  107. WIFI_NETWORKS_FILE=${1}
  108. ;;
  109. --disable)
  110. shift
  111. WIFI_DISABLE=${1}
  112. if [[ $WIFI_DISABLE == $'yes' || $WIFI_DISABLE == $'y' ]]; then
  113. WIFI_DISABLE='yes'
  114. else
  115. WIFI_DISABLE='no'
  116. fi
  117. ;;
  118. *)
  119. # unknown option
  120. ;;
  121. esac
  122. shift
  123. done
  124. if [ ${NETWORKS_INTERACTIVE} ]; then
  125. create_networks_interactive
  126. exit 0
  127. fi
  128. if [ ! ${wifi_interface_specified} ]; then
  129. if [ ! $WAIT_SEC ]; then
  130. wpa_action ${WIFI_INTERFACE} stop
  131. wpa_cli -i ${WIFI_INTERFACE} terminate
  132. else
  133. sleep ${WAIT_SEC}
  134. fi
  135. update_wifi_adaptors
  136. if [ ! $IFACE ]; then
  137. echo $'No wifi adaptors were found'
  138. exit 872356
  139. fi
  140. WIFI_INTERFACE=${IFACE}
  141. echo "Adaptor: $WIFI_INTERFACE"
  142. write_config_param "WIFI_INTERFACE" "$WIFI_INTERFACE"
  143. fi
  144. if [ ${WIFI_DISABLE} ]; then
  145. disable_wifi ${WIFI_DISABLE}
  146. remove_config_param "WIFI_INTERFACE"
  147. exit 0
  148. fi
  149. if [ -f ${WIFI_NETWORKS_FILE} ]; then
  150. networks_from_file
  151. wpa_cli status
  152. exit 0
  153. fi
  154. if [ ! ${WIFI_SSID} ]; then
  155. echo $'No SSID given'
  156. exit 1
  157. fi
  158. if [[ ${WIFI_HOTSPOT} != 'no' ]]; then
  159. hotspot_on
  160. exit 0
  161. else
  162. hotspot_off
  163. fi
  164. if [[ "$WIFI_TYPE" != 'none' && "$WIFI_TYPE" != 'open' ]]; then
  165. if [ ! $WIFI_PASSPHRASE ]; then
  166. echo $'No wifi passphrase was given'
  167. exit 2
  168. fi
  169. fi
  170. if [[ ${WIFI_TYPE} == 'wpa2-psk' ]]; then
  171. if [ ! -d /etc/wpa_supplicant ]; then
  172. echo $'wpasupplicant package is not installed'
  173. exit 3
  174. fi
  175. wifi_wpa2_psk "$WIFI_SSID" "$WIFI_PASSPHRASE"
  176. exit 0
  177. fi
  178. if [[ "$WIFI_TYPE" == 'none' || "$WIFI_TYPE" == 'open' ]]; then
  179. wifi_none "$WIFI_SSID"
  180. exit 0
  181. fi
  182. exit 0