document.write('
Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. #!/bin/bash
  2.  
  3. #hostapd.sh -i INTERFACE [-s "SSID" -k "KEY" -a IP_ADDR -c CHANNEL -d DRIVER -f "CONF_FILE.hostap" -x CONF_COPY]
  4. #
  5. # RUNNING THIS SCRIPT
  6. # Minimum mandatory command line arguments using a previously saved file:
  7. # -f CONF_FILE
  8. #
  9. #
  10. # Minimum mandatory command line arguments using NO saved file and built-in defaults
  11. # specified
  12. # -i INTERFACE
  13. #
  14. #
  15. # OPTIMAL minimum command line arguments using NO saved file
  16. # specified
  17. # -i INTERFACE
  18. # -s SSID
  19. # -k KEY
  20. # -a IP_ADDR
  21. #
  22. #
  23. # Optional arguments, if not specified will revert to defaults listed
  24. # -s SSID
  25. # -k KEY
  26. # -a IP_ADDR
  27. # -c CHANNEL
  28. # -d DRIVER
  29. #
  30. #
  31. # -x CONF_COPY, copies/overwrites the completed .hostapd file to /etc/hostapd/hostapd.conf
  32. # and nothing else afterwards.  Any previous config file is saved as hostapd.conf.old
  33. # Ensure that you edit the following line in the file /etc/init.d/hostapd
  34. # DAEMON_CONF=
  35. # TO this (typical /etc/hostapd/hostapd.conf)
  36. # DAEMON_CONF=/path/to/hostapd.conf
  37. # Then run the commands:
  38. # $ chmod + x hostapd
  39. # $ innserv /etc/init.d/hostapd
  40. #
  41. #
  42. # Remember to start a DHCP Server if you need to assign ip addresses to your clients.
  43. #
  44.  
  45. #######################################################################
  46. ############################FUNCTIONS##################################
  47.  
  48. function CONF_COPY(){
  49.     #
  50.     # This function accepts the configuration filename as the first parameter.
  51.     #
  52.     # Is $1 parameter (file name) zero length?
  53.     if [ -z "$1" ]; then
  54.         printf "No File Name specified for configuration copying!\\nExiting...\\n"
  55.     else
  56.         #
  57.         # Check /etc/hostapd folder
  58.         #
  59.         if [ ! -d "/etc/hostapd" ]; then
  60.             printf "The /etc/hostapd folder does not exist!\\nExiting...\\n"
  61.         exit 1
  62.         fi
  63.         #
  64.         # Check if config file exists, move file to hostapd.conf.old if it does
  65.         #
  66.         if [ -a /etc/hostapd/hostapd.conf ]; then
  67.             printf "Moving current hostapd.conf file to hostapd.conf.old\\n"
  68.             mv /etc/hostapd/hostapd.conf /etc/hostapd/hostapd.conf.old
  69.             #
  70.             # Check if config file has been moved to hostapd.conf.old
  71.             #
  72.             if [ -a /etc/hostapd/hostapd.conf ]; then
  73.                 printf "Could not move hostapd.conf file!\\nExiting...\\n"
  74.                 exit 1
  75.             fi
  76.         fi
  77.         #
  78.         # Copy local config file to /etc/hostapd/hostapd.conf
  79.         #
  80.         printf "Copying file $1 to /etc/hostapd/hostapd.conf\\n"
  81.         cp ./"$1" /etc/hostapd/hostapd.conf
  82.         exit 0
  83.     fi
  84. }
  85.  
  86.  
  87. function START_SERVICE (){
  88.     #
  89.     # This function accepts the configuration filename as the first parameter.
  90.     #
  91.     if [ -n "$1" ]; then
  92.             if [ -s "$1" ]; then
  93.                 printf "Config file $1 exists, proceeding to start service.\\n"
  94.                 hostapd -B "$1"
  95.                 exit 0
  96.             else    printf "Config file $1 is invalid or does not exist\\nExiting...\\n"
  97.                 exit 1
  98.             fi
  99.         fi
  100. }
  101.  
  102. function READ_SAVED (){
  103.     #
  104.     # Search or uses for previous hostap configuration files
  105.     # Checks if command line argument -f "CONF_FILE.hostap" is greater than zero length and exists
  106.     # before to function CREATE_HOST.  Otherwise exits.
  107.     #
  108.     if [ -n "$SAVED_FILE" ]; then
  109.         if [ -s "$SAVED_FILE" ]; then
  110.             printf "Config file $SAVED_FILE exists, proceeding...\\n"
  111.             #
  112.             # Choose to copy config to /etc/hostapd/hostapd.conf or start hostapd with
  113.             # local config file.
  114.             #
  115.             if [ $CONFIG_COPY ]; then
  116.                 CONF_COPY "$SAVED_FILE"
  117.             else
  118.                 START_SERVICE "$SAVED_FILE"
  119.                 exit 0
  120.             fi
  121.         else    printf "Config file $SAVED_FILE is invalid or does not exist!\\nExiting...\\n"
  122.             exit 1
  123.         fi
  124.     fi
  125. }
  126.  
  127. function WRITE_CONF (){
  128.     printf "Writing new configuration file\\n"
  129.  
  130.     FILENAME="$SSID.hostapd"
  131.  
  132.     printf "$INT $SSID $KEY $ADDR $CHANNEL $DRIVER $FILENAME\\n"
  133.  
  134.     echo "# This file contains a hostapd.conf configuration generated by hostapd_script.sh" > "${FILENAME}"
  135.     echo "interface=$INT" >> "${FILENAME}"
  136.     echo "ssid=$SSID" >> "${FILENAME}"  # refer to documentation for ssid2 usage, use here if works
  137.     echo "wpa_passphrase=$KEY" >> "${FILENAME}"
  138.     echo "channel=$CHANNEL" >> "${FILENAME}"
  139.     echo "driver=$DRIVER" >> "${FILENAME}"
  140.     echo "$DEFAULT_EXTRAS" >> "${FILENAME}"
  141.     #
  142.     # Choose to copy config to /etc/hostapd/hostapd.conf or start hostapd with
  143.     # local config file.
  144.     #
  145.     if [ $CONFIG_COPY ]; then
  146.         CONF_COPY "$FILENAME"
  147.     else
  148.         START_SERVICE "$FILENAME"
  149.         exit 1
  150.     fi
  151. }
  152.  
  153.  
  154. #######################################################################
  155. ############################START HERE#################################
  156. #
  157. # Declaration of Optional Command Line Defaults
  158. #
  159. DEFAULT_INTERFACE="wlan0"
  160. DEFAULT_CHANNEL="6"
  161. DEFAULT_DRIVER="nl80211"
  162. DEFAULT_SSID="HACK ME"
  163. DEFAULT_KEY="12345678"
  164. DEFAULT_ADDR="192.168.0.1"
  165. DEFAULT_SUBNET="255.255.255.0"
  166. #
  167. # Recommended other options that should be default, these are adopted from hostapd documentation
  168. # and help make the AP work.
  169. #
  170. DEFAULT_EXTRAS=$\'wpa=2\\nmacaddr_acl=0\\nauth_algs=1\\nignore_broadcast_ssid=0\\nwpa_key_mgmt=WPA-PSK\\nwpa_pairwise=TKIP\\nrsn_pairwise=CCMP\\n\'
  171.  
  172.  
  173. #
  174. # Give useage if no arguments are specified
  175. #
  176. if [ -z "$1" ]; then
  177. printf "Usage: $0 -i INTERFACE [-s \\"SSID\\" -k \\"KEY\\" -a IP_ADDR -c CHANNEL -d DRIVER -f \\"CONF_FILE.hostap\\" -x CONF_COPY]\\n"
  178. exit 1
  179. fi
  180.  
  181. #
  182. # Check if root
  183. #
  184. if [ "$(id -u)" != "0" ]; then
  185. printf "This script must be run as root\\n" 1>&2
  186. exit 1
  187. fi
  188.  
  189. #
  190. # Using getopts to phrase command line arguments
  191. # Getopts; put a ":" AFTER something you want a argument to follow a option
  192. #
  193. while getopts "i:s:k:a:c:d:e:f:x" opt
  194.     do
  195.     case $opt in
  196.     i ) INT=${OPTARG};;
  197.     s ) SSID=${OPTARG};;
  198.     k ) KEY=${OPTARG};;
  199.     a ) ADDR=${OPTARG};;
  200.     c ) CHANNEL=${OPTARG};;
  201.     d ) DRIVER=${OPTARG};;
  202.     f ) SAVED_FILE=${OPTARG};;
  203.     x ) CONFIG_COPY=1;;
  204.     \\?) printf "Usage: $0 -i INTERFACE [-s \\"SSID\\" -k \\"KEY\\" -a IP_ADDR -c CHANNEL -d DRIVER -f \\"CONF_FILE.hostap\\" -x CONF_COPY]\\n"
  205.         exit 1;;
  206.     * ) printf "Usage: $0 -i INTERFACE [-s \\"SSID\\" -k \\"KEY\\" -a IP_ADDR -c CHANNEL -d DRIVER -f \\"CONF_FILE.hostap\\" -x CONF_COPY]\\n"
  207.         exit 1;;
  208.     esac
  209. done
  210.  
  211. #
  212. # Assign default variables to unspecified command line arguments.
  213. #
  214. : ${INT:=$DEFAULT_INTERFACE}
  215. : ${KEY:=$DEFAULT_KEY}
  216. : ${ADDR:=$DEFAULT_ADDR}
  217. : ${CHANNEL:=$DEFAULT_CHANNEL}
  218. : ${DRIVER:=$DEFAULT_DRIVER}
  219. : ${SSID:=$DEFAULT_SSID}
  220. : ${SUBNET:=$DEFAULT_SUBNET}
  221.  
  222. #
  223. # Check if -i interface is entered as command line argument, mandatory
  224. #
  225. if [ -n "$INT" -a -n "$SAVED_FILE" ]; then
  226. printf "Ignoring -i $INT as interface is already specified in file $SAVED_FILE\\n"
  227. fi
  228.  
  229. #printf "DEBUG:\\nINT is $INT, SSID is $SSID, KEY is $KEY, ADDR is $ADDR\\nChannel is $CHANNEL, Driver is $DRIVER, Save File is $SAVEFILE\\n"
  230.  
  231. #
  232. # check if file option is entered as command line argument, if yes jump
  233. # to the create_host function
  234. #
  235. if [ -n "$SAVED_FILE" ]; then
  236.     printf "Using saved file $SAVED_FILE\\n"
  237.         READ_SAVED
  238. else
  239.     printf "Using command line parameters and or defaults\\n"
  240.     WRITE_CONF
  241. exit 1
  242. fi
');