Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- #hostapd.sh -i INTERFACE [-s "SSID" -k "KEY" -a IP_ADDR -c CHANNEL -d DRIVER -f "CONF_FILE.hostap" -x CONF_COPY]
- #
- # RUNNING THIS SCRIPT
- # Minimum mandatory command line arguments using a previously saved file:
- # -f CONF_FILE
- #
- #
- # Minimum mandatory command line arguments using NO saved file and built-in defaults
- # specified
- # -i INTERFACE
- #
- #
- # OPTIMAL minimum command line arguments using NO saved file
- # specified
- # -i INTERFACE
- # -s SSID
- # -k KEY
- # -a IP_ADDR
- #
- #
- # Optional arguments, if not specified will revert to defaults listed
- # -s SSID
- # -k KEY
- # -a IP_ADDR
- # -c CHANNEL
- # -d DRIVER
- #
- #
- # -x CONF_COPY, copies/overwrites the completed .hostapd file to /etc/hostapd/hostapd.conf
- # and nothing else afterwards. Any previous config file is saved as hostapd.conf.old
- # Ensure that you edit the following line in the file /etc/init.d/hostapd
- # DAEMON_CONF=
- # TO this (typical /etc/hostapd/hostapd.conf)
- # DAEMON_CONF=/path/to/hostapd.conf
- # Then run the commands:
- # $ chmod + x hostapd
- # $ innserv /etc/init.d/hostapd
- #
- #
- # Remember to start a DHCP Server if you need to assign ip addresses to your clients.
- #
- #######################################################################
- ############################FUNCTIONS##################################
- function CONF_COPY(){
- #
- # This function accepts the configuration filename as the first parameter.
- #
- # Is $1 parameter (file name) zero length?
- if [ -z "$1" ]; then
- printf "No File Name specified for configuration copying!\nExiting...\n"
- else
- #
- # Check /etc/hostapd folder
- #
- if [ ! -d "/etc/hostapd" ]; then
- printf "The /etc/hostapd folder does not exist!\nExiting...\n"
- exit 1
- fi
- #
- # Check if config file exists, move file to hostapd.conf.old if it does
- #
- if [ -a /etc/hostapd/hostapd.conf ]; then
- printf "Moving current hostapd.conf file to hostapd.conf.old\n"
- mv /etc/hostapd/hostapd.conf /etc/hostapd/hostapd.conf.old
- #
- # Check if config file has been moved to hostapd.conf.old
- #
- if [ -a /etc/hostapd/hostapd.conf ]; then
- printf "Could not move hostapd.conf file!\nExiting...\n"
- exit 1
- fi
- fi
- #
- # Copy local config file to /etc/hostapd/hostapd.conf
- #
- printf "Copying file $1 to /etc/hostapd/hostapd.conf\n"
- cp ./"$1" /etc/hostapd/hostapd.conf
- exit 0
- fi
- }
- function START_SERVICE (){
- #
- # This function accepts the configuration filename as the first parameter.
- #
- if [ -n "$1" ]; then
- if [ -s "$1" ]; then
- printf "Config file $1 exists, proceeding to start service.\n"
- hostapd -B "$1"
- exit 0
- else printf "Config file $1 is invalid or does not exist\nExiting...\n"
- exit 1
- fi
- fi
- }
- function READ_SAVED (){
- #
- # Search or uses for previous hostap configuration files
- # Checks if command line argument -f "CONF_FILE.hostap" is greater than zero length and exists
- # before to function CREATE_HOST. Otherwise exits.
- #
- if [ -n "$SAVED_FILE" ]; then
- if [ -s "$SAVED_FILE" ]; then
- printf "Config file $SAVED_FILE exists, proceeding...\n"
- #
- # Choose to copy config to /etc/hostapd/hostapd.conf or start hostapd with
- # local config file.
- #
- if [ $CONFIG_COPY ]; then
- CONF_COPY "$SAVED_FILE"
- else
- START_SERVICE "$SAVED_FILE"
- exit 0
- fi
- else printf "Config file $SAVED_FILE is invalid or does not exist!\nExiting...\n"
- exit 1
- fi
- fi
- }
- function WRITE_CONF (){
- printf "Writing new configuration file\n"
- FILENAME="$SSID.hostapd"
- printf "$INT $SSID $KEY $ADDR $CHANNEL $DRIVER $FILENAME\n"
- echo "# This file contains a hostapd.conf configuration generated by hostapd_script.sh" > "${FILENAME}"
- echo "interface=$INT" >> "${FILENAME}"
- echo "ssid=$SSID" >> "${FILENAME}" # refer to documentation for ssid2 usage, use here if works
- echo "wpa_passphrase=$KEY" >> "${FILENAME}"
- echo "channel=$CHANNEL" >> "${FILENAME}"
- echo "driver=$DRIVER" >> "${FILENAME}"
- echo "$DEFAULT_EXTRAS" >> "${FILENAME}"
- #
- # Choose to copy config to /etc/hostapd/hostapd.conf or start hostapd with
- # local config file.
- #
- if [ $CONFIG_COPY ]; then
- CONF_COPY "$FILENAME"
- else
- START_SERVICE "$FILENAME"
- exit 1
- fi
- }
- #######################################################################
- ############################START HERE#################################
- #
- # Declaration of Optional Command Line Defaults
- #
- DEFAULT_INTERFACE="wlan0"
- DEFAULT_CHANNEL="6"
- DEFAULT_DRIVER="nl80211"
- DEFAULT_SSID="HACK ME"
- DEFAULT_KEY="12345678"
- DEFAULT_ADDR="192.168.0.1"
- DEFAULT_SUBNET="255.255.255.0"
- #
- # Recommended other options that should be default, these are adopted from hostapd documentation
- # and help make the AP work.
- #
- 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'
- #
- # Give useage if no arguments are specified
- #
- if [ -z "$1" ]; then
- printf "Usage: $0 -i INTERFACE [-s \"SSID\" -k \"KEY\" -a IP_ADDR -c CHANNEL -d DRIVER -f \"CONF_FILE.hostap\" -x CONF_COPY]\n"
- exit 1
- fi
- #
- # Check if root
- #
- if [ "$(id -u)" != "0" ]; then
- printf "This script must be run as root\n" 1>&2
- exit 1
- fi
- #
- # Using getopts to phrase command line arguments
- # Getopts; put a ":" AFTER something you want a argument to follow a option
- #
- while getopts "i:s:k:a:c:d:e:f:x" opt
- do
- case $opt in
- i ) INT=${OPTARG};;
- s ) SSID=${OPTARG};;
- k ) KEY=${OPTARG};;
- a ) ADDR=${OPTARG};;
- c ) CHANNEL=${OPTARG};;
- d ) DRIVER=${OPTARG};;
- f ) SAVED_FILE=${OPTARG};;
- x ) CONFIG_COPY=1;;
- \?) printf "Usage: $0 -i INTERFACE [-s \"SSID\" -k \"KEY\" -a IP_ADDR -c CHANNEL -d DRIVER -f \"CONF_FILE.hostap\" -x CONF_COPY]\n"
- exit 1;;
- * ) printf "Usage: $0 -i INTERFACE [-s \"SSID\" -k \"KEY\" -a IP_ADDR -c CHANNEL -d DRIVER -f \"CONF_FILE.hostap\" -x CONF_COPY]\n"
- exit 1;;
- esac
- done
- #
- # Assign default variables to unspecified command line arguments.
- #
- : ${INT:=$DEFAULT_INTERFACE}
- : ${KEY:=$DEFAULT_KEY}
- : ${ADDR:=$DEFAULT_ADDR}
- : ${CHANNEL:=$DEFAULT_CHANNEL}
- : ${DRIVER:=$DEFAULT_DRIVER}
- : ${SSID:=$DEFAULT_SSID}
- : ${SUBNET:=$DEFAULT_SUBNET}
- #
- # Check if -i interface is entered as command line argument, mandatory
- #
- if [ -n "$INT" -a -n "$SAVED_FILE" ]; then
- printf "Ignoring -i $INT as interface is already specified in file $SAVED_FILE\n"
- fi
- #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"
- #
- # check if file option is entered as command line argument, if yes jump
- # to the create_host function
- #
- if [ -n "$SAVED_FILE" ]; then
- printf "Using saved file $SAVED_FILE\n"
- READ_SAVED
- else
- printf "Using command line parameters and or defaults\n"
- WRITE_CONF
- exit 1
- fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement