Posted By

mladoux on 09/21/10


Tagged

Bash linux cli utility wireless


Versions (?)

wlassist 1.3


 / Published in: Bash
 

URL: http://mark.haktstudios.com

A simple command line wireless network manager written in Bash for Linux that I wrote a while ago. This is one of those scripts that I've used forever because it simply just works. Requires Linux wireless utilities, Bash, WPA Utilities, and a some minor system specific configurations. The default settings should work well on MOST systems, however, on some systems you will need to change the wireless driver used.

  1. #!/bin/bash
  2.  
  3. ################################################################
  4. ## PROJECT #####################################################
  5. ################################################################
  6. ## Date : 09/21/2007 ##
  7. ## Version : 1.3 ##
  8. ## Program : wlassist ##
  9. ## Author : Mark A. LaDoux ##
  10. ## Website :http://www.haktstudios.com ##
  11. ## E-Mail : [email protected] ##
  12. ################################################################
  13. ## DESCRIPTION #################################################
  14. ################################################################
  15. ## Console application to simplify the maintainence of ##
  16. ## wireless network settings. It stores configurations and ##
  17. ## recalls them when requested. It is capable of handling ##
  18. ## unencrypted, WEP, and WPA encrypted networks. ##
  19. ################################################################
  20. ## DEPENDANCIES ################################################
  21. ################################################################
  22. ## Wireless capable device ##
  23. ## bash 2.x or better ##
  24. ## linux wireless utils (for configuring wireless card) ##
  25. ## wpa_supplicant (for accessing wpa networks) ##
  26. ## permissions to modify network settings ##
  27. ################################################################
  28. ## COPYRIGHT ###################################################
  29. ################################################################
  30. ## ©2007 Hakt Studios. http://www.haktstudios.com ##
  31. ## ##
  32. ## This program is free software; you can redistribute it ##
  33. ## and/or modify it under the terms of the GNU General ##
  34. ## Public License as published by the Free Software ##
  35. ## Foundation; either version 3 of the License, or (at your ##
  36. ## option) any later version. ##
  37. ## ##
  38. ## This program is distributed in the hope that it will be ##
  39. ## useful, but WITHOUT ANY WARRANTY; without even the implied ##
  40. ## warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR ##
  41. ## PURPOSE. See the GNU General Public License for more ##
  42. ## details. ##
  43. ## ##
  44. ## You should have received a copy of the GNU General Public ##
  45. ## License along with this program. If not, see ##
  46. ## <http://www.gnu.org/licenses/>. ##
  47. ################################################################
  48.  
  49. ################################################################
  50. ## Ok to start we are going to define a couple of things ##
  51. ## please adjust thes variables to match your hardware ##
  52. DRIVER=wext ## This is the driver to use for wpa_supplicant ##
  53. IFACE=wlan0 ## this is the network interface to use ##
  54. ################################################################
  55.  
  56. ################################################################
  57. ## DO EDIT ANYTHING BELOW THIS POINT ##
  58. ################################################################
  59.  
  60. RUID=0 # root uid
  61. WDIR=/etc/wlassist # where settings are stored
  62.  
  63. # Ensure that search path is defined - sometimes when using su or
  64. # sudo it doesn't get set properly, and that causes errors with
  65. # the script.
  66.  
  67. PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin
  68. export PATH
  69.  
  70. ################################################################
  71. ## ERROR CODES ##
  72. ################################################################
  73. E_NR=65 # not root
  74. E_IO=66 # invalid option
  75. E_NF=67 # Not Found
  76. E_UC=68 # User Canceled
  77.  
  78. ###
  79. # Ask user if they would like to proceed to the next step
  80. function CONTINUE(){
  81. echo "Would you like to continue? (Y/n)"
  82. read YESNO
  83. if [ "$YESNO" == "N" ] || [ "$YESNO" == "n" ]
  84. then
  85. echo "Terminating program per user request..."
  86. exit "$E_UC"
  87. fi
  88. }
  89.  
  90. ###
  91. # Select Wireless Encryption Method
  92. function ENCRYPT(){
  93. echo "What encryption does $ESSID use?"
  94. echo ""
  95. echo "1) none"
  96. echo "2) WEP"
  97. echo "3) WPA"
  98. CFILE=$WFILE.conf
  99. read CRYPT
  100. case "$CRYPT" in
  101. ## No Encryption
  102. "1" )
  103. echo "You have chosen to not use encryption..."
  104. CONTINUE
  105. echo "Saving settings...."
  106. echo "#!/bin/bash" >> "$WFILE"
  107. echo "iwconfig $IFACE essid $ESSID" >> "$WFILE"
  108. echo "dhclient $IFACE" >> "$WFILE"
  109. chmod +x "$WFILE"
  110. echo "Initializing connection"
  111. "$WFILE"
  112. exit 0
  113. ;;
  114. ## WEP Encryption
  115. "2" )
  116. echo "You have chosen to use WEP..."
  117. CONTINUE
  118. echo "Ok, now I would like to collect some info from you."
  119. echo "What is your WEP Key?"
  120. read KEY
  121. echo "Please double check the key for errors before continuing."
  122. echo "$KEY"
  123. CONTINUE
  124. echo "Saving settings..."
  125. echo "#!/bin/bash" >> "$WFILE"
  126. echo "iwconfig $IFACE essid $ESSID key $KEY" >> "$WFILE"
  127. echo "dhclient $IFACE" >> "$WFILE"
  128. chmod +x $WFILE
  129. echo "Initializing connection"
  130. "$WFILE"
  131. exit 0
  132. ;;
  133. ## WPA-PSK Encryption
  134. "3")
  135. echo "You have chosen to use WPA..."
  136. CONTINUE
  137. echo "Okay, now I would like to collect some info from you"
  138. echo "What is your WPA passphrase?"
  139. read PSK
  140. echo "Please double check your passphrase for errors before continuing."
  141. echo "$PSK"
  142. CONTINUE
  143. echo "Saving settings..."
  144. ## begin conf file
  145. echo "network={" >> "$CFILE"
  146. echo " ssid=\"$ESSID\"" >> "$CFILE"
  147. echo " psk=\"$PSK\"" >> "$CFILE"
  148. echo "}" >> "$CFILE"
  149. ## begin script file
  150. echo "#!/bin/bash" >> $WFILE
  151. echo "wpa_supplicant -D$DRIVER -i$IFACE -c$CFILE &" >> "$WFILE"
  152. echo "dhclient $IFACE" >> "$WFILE"
  153. chmod +x $WFILE
  154. echo "Initializing connection"
  155. "$WFILE"
  156. exit 0
  157. ;;
  158. ## Invalid Selection
  159. * )
  160. echo "Invalid option, exiting program..."
  161. exit "$E_IO"
  162. ;;
  163. esac
  164.  
  165. }
  166.  
  167. ## Program Begins Here ##
  168.  
  169. # Check to make sure user has permission to modify network settings
  170. if [ "$RUID" != "$UID" ]
  171. then
  172. echo "I'm sorry, but you don't seem to have proper permission"
  173. echo "to perform this action"
  174. exit "$E_NR"
  175. fi
  176.  
  177. # Check for configuration directory
  178. # if it doesn't exist, create it
  179. if [ ! -e "$WDIR" ]
  180. then
  181. mkdir "$WDIR"
  182. fi
  183.  
  184. # Fix for systems where the wireless is down by default
  185. ifconfig "$IFACE" up
  186.  
  187. # Ask if user wants to scan
  188. echo "Would you like to scan available networks? (y|N)"
  189. read NOYES
  190.  
  191. if [ "$NOYES" == "Y" ] || [ "$NOYES" == "y" ]
  192. then
  193. iwlist "$IFACE" scan | less
  194. fi
  195.  
  196. # Get the Network Name
  197. echo "Which network would you like to connect to today?"
  198. read ESSID
  199. WFILE=$WDIR/$ESSID
  200.  
  201. # See if configuration already exists for this network
  202. # if it doesn't, proceed to creating a new one
  203. if [ ! -e "$WFILE" ]
  204. then
  205. echo "No configuration for $ESSID currently exists!"
  206. CONTINUE
  207. ENCRYPT
  208. fi
  209.  
  210. # Execute network configuration
  211. $WFILE
  212. exit 0

Report this snippet  

You need to login to post a comment.