Friday 14 June 2013

UPDATE: Modern Bash Wifi Connection Script for Linux; goodbye network manager

An update to a previous post.  This script has some more command line features, such as calling up previous saved sessions credentials from the command line and improved error handling.  Completely run from the command line.

View it here: http://pastebin.com/J229Gc1t

or below.


#!/bin/bash

# http://pastebin.com/J229Gc1t
# 12/06/2013
# This script attempts to semi-automate the wifi connection process from
# the command line.
# It is intended to be used on a headless machine without the
# requirement of typing several commands for a connection.
# The script stores previous connection credentials in PLAINTEXT as
# *.wpa files in the executed directory and in /etc/wpasupplicant.conf.
# These .wpa files are used to connect to several different ap using
# previously stored info.
# Probably a good idea to stop and other network managing software while
# running this script, also in testing wpa_supplicant does a pretty good
# job of re-connecting a disassociated link automatically.
#
# Mainly created from a combination of scripts taken from theses two
# sources:
# http://www.backtrack-linux.org/forums/archive/index.php/t-3367.html
# AND
# http://www.linuxquestions.org/questions/linux-general-1/wifi-connect\
# -script-tested-in-ubuntu-772646/
#
# old version1 http://pastebin.com/Pa90HBiU 01/06/2013
# very simple first version
# old version2 http://pastebin.com/FzJnv5Nk 02/06/2013
# minor additions
# old version3 http://pastebin.com/3mu1XT5Y 08/06/2013
# included ability to call up previous saved files from
# command line. Some checking of command line parameters and
# checking for empty saved files
# current version
# Added exit retrurn values
#
# Copy, Distribute and Modify Freely.
#


if [ -z "$1" ]; then
printf "Usage: $0 -i [interface] OR AND -f [SAVED_FILE.wpa]\n"
exit 1
fi

while getopts "i:f:" opt
do
case $opt in
i ) INT=${OPTARG};;
f ) ITEM=${OPTARG};;
\?) printf "Usage: $0 -i [interface] OR AND -f [SAVED_FILE.wpa]\n"
exit 1;;
* ) printf "Usage: $0 -i [interface] OR AND -f [SAVED_FILE.wpa]\n"
exit 1;;
esac
done

#
# check if root
#
if [ "$(id -u)" != "0" ]; then
printf "This script must be run as root\n" 1>&2
exit 1
fi

#
# check if interface is entered as command line argument
#
if [ -z "$INT" ]; then
printf "Usage: $0 -i [interface] OR AND -f [SAVED_FILE.wpa]\n"
exit 1
fi


#
# Search for previous saved config files
#
function read_saved (){
#
# Search or uses for previous wpa configuration files
# Checks if command line argument -f [SAVED_FILE.wpa] is greater than zero length and exists
# before writing the configiuration to wpa_supplicant.conf. Otherwise create a new
# configuration.
#
if [ -n "$ITEM" ]; then
if [ -s "$ITEM" ]; then
printf "File $ITEM exists, proceeding to connect\n"
write_conf $ITEM
else printf "File $ITEM is invalid or does not exist, proceeding to create a new configuration\n"
conf_create
fi
fi
#
# Save and change IFS so spaces in file names are not interpreted as
# separate lines in the array
#
OLDIFS=$IFS
IFS=$'\n'

#
# Read all file names into an array
# ref:http://www.cyberciti.biz/tips/handling-filenames-with-spaces\
# -in-bash.html
#
# " -printf '%f\n' " removes path info from outputs
#
# ref:http://serverfault.com/questions/354403/remove-path-from-find\
# -command-output
#
SAVED_LIST=($(find . -type f -name "*.wpa" -printf '%f\n'))

#
# restore ifs
#
IFS=$OLDIFS


#
# Tests for number of saved wifi connections, if none exit
#
if [ -z "${SAVED_LIST[0]}" ]; then
printf "There are no previous saved wifi connections\n"
#
# Create new connection
#
conf_create
fi

#
#PS3 Sets the prompt for the select statement below
#
PS3="Choose a previously saved wifi connection or 's' to skip: "

#
#Select one of the previous saved configurations to connect with or quit
#
select ITEM in "${SAVED_LIST[@]}"; do
#
# Quit if selected number does not exist or alpha in entered
#
if [ -z "$ITEM" ] ; then
printf "Skipping\n\n"
conf_create
fi
#
# Quick check if file is greater than zero length and exists
#
if [ -s "$ITEM" ]; then
printf "File $ITEM exists, proceeding to connect\n"
write_conf "$ITEM"
else printf "File $ITEM is invalid or does not exist, proceeding to create a new configuration\n"
conf_create
fi
done
}


function conf_create (){
#
# Scans for wifi connections & isolates wifi AP name
#
eval LIST=( $(iwlist $INT scan 2>/dev/null | awk -F":" '/ESSID/{print $2}') )

#
#PS3 Sets the prompt for the select statement below
#
PS3="Choose wifi connection or 'q' to quit: "

#
# Tests for number of wifi connections, exits if none
#
if [ -z "${LIST[0]}" ]; then
printf "No available wifi connection using $INT\n"
exit 1
fi

#
# Select from a LIST of scanned connections
#
select ITEM in "${LIST[@]}"; do

#
# Quit if selected number does not exist or alpha in entered
#
if [ -z "$ITEM" ] ; then
printf "Exiting\n"
exit 0
fi

#
# Get user input for passphrase no need to escape spaces
#
printf "Enter the passphrase for $ITEM?\n"
read "PASSPHRASE"

#
# Append the ITEM variable (ESSID) to .wpa to make a filename
# for saved configs
#
FILENAME=$ITEM".wpa"

#
# Run wpa_passphrase to generate a file for wpa_supplicant to
# use, store it locally and in etc/wpa_supplicant.conf
#
printf "Running wpa_passphrase\n"
wpa_passphrase "$ITEM" "$PASSPHRASE" > "$FILENAME" | xargs
#
# Jump to write_conf function, append configuration filename
# to ITEM varibale
#
ITEM="$FILENAME"
write_conf
done
}

function write_conf (){
#
# Copy local wpa_supplicant file to etc/wpa_supplicant.conf
#
printf "Writing new configuration file using $ITEM\n"
cat "$ITEM">/etc/wpa_supplicant.conf | xargs
#
# Jump to connect function, pass on the ESSID variable for connection
#
connect "$ITEM"
}

function connect (){
printf "Connecting using file $*\n"

#
# Capture incoming argument
#
ESSID=$*

#
# Kill previous instances of wpa_supplicant to stop other instances
# wpa_supplicant fighting several different AP's
# Kill based on
# ref: http://thegeekstuff.com/2011/10/grep-or-and-not-operators
# and
# http://stackoverflow.com/questions/3510673/find-and-kill-a-\
# process-in-one-line-using-bash-and-regex
#
# Release dhcp ip's and bring down the interface
#
kill $(ps aux | grep -E '[w]pa_supplicant.*\'$INT'' | awk '{print $2}') 2>/dev/null | xargs
dhclient $INT -r
ifconfig $INT down

#
# Assign new credentials to interface
#
iwconfig $INT mode managed essid "$ESSID"
printf "Configured interface $INT; Credential file is $ESSID\n"
ifconfig $INT up
printf "Interface $INT is up\n"
wpa_supplicant -B -Dwext -i$INT -c/etc/wpa_supplicant.conf 2>/dev/null | xargs
printf "wpa_supplicant running, sleeping for 15...\n"

#
# Wait to connect before asking for a ip address
#
sleep 15
printf "Running dhclient\n"
dhclient $INT

#
# Show current ip for interface
#
ifconfig $INT | grep inet
exit 0
}

function clean_slate (){
#
# Optional Clean Slate commands, it is recommended that you perform
# a "airmon-ng check kill" to ensure that any other network managers
# do not interfere with the connection process
#

printf "It is recommended that you perform a \"airmon-ng check kill\" once to ensure that any other network managers do not interfere with the connection process\n\n"

#
# Untested, airmon-ng check kill works better here
#
# service network-manager stop 2>/dev/null >/dev/null
# service avahi-daemon stop 2>/dev/null >/dev/null
# sleep 10
# killall wpa_supplicant 2>/dev/null
# ifconfig $INT up
}

#
# Start here
#
clean_slate
read_saved

exit 0




WindyCityTech Blogger
WindyWindyCityTech Wordpress

No comments:

Post a Comment