Modern Bash Wifi Connection Script for Linux; goodbye network manager

This script attempts to semi-automate the wifi connection process from
the command line.  It is intended to be used on a headless / GUI-less machine without the
requirement of typing several commands for a connection.  For instance a Raspberry PI.

It works reasonably well for most applications and can handle ESSID’s / passwords with spaces.

Storage of previous connection credentials is in PLAINTEXT as [ESSID].wpa files in the executed directory and in /etc/wpasupplicant.conf.
These .wpa files are used to connect to several different AP’s using previously stored info.
Probably a real good idea to stop and other network managing software while running this script, for instance running “airmon-ng check kill” will do a good job.
Also in testing wpa_supplicant does a pretty good job of re-connecting a disassociated link automatically.

Operation is simple.  In the case with Debian it is:
sudo ./connect.sh wlan0

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/

Feel free to comment or send improvements.

Download from http://pastebin.com/FzJnv5Nk
or below.

#!/bin/bash

# http://pastebin.com/Pa90HBiU
# 01/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 version http://pastebin.com/Pa90HBiU 01/06/2013
#
# Copy, Distribute and Modify Freely.
#

INT=$1

if [ -z "$1" ]; then
printf "Usage: $0 [interface]\n"
exit
fi

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

#
# Search for previous saved config files
#
function read_saved {
#
# Search for previous wpa configuration files.
#

#
# 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\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"
conf_create
fi

printf "$ITEM is selected\n"
cat "$ITEM">/etc/wpa_supplicant.conf | xargs
connect "$ITEM"
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
fi

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

#
# Quit if selected number does not exist or alpha in entered
#
if [ -z "$ITEM" ] ; then
printf "Exiting\n"
exit
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
cat "$FILENAME">/etc/wpa_supplicant.conf | xargs
printf "Creating new configuration using $ITEM\n"

#
# Jump to connect function, pass on the ESSID variable for connection
#
connect $ITEM
done
}

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; ESSID 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
}

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 intefere 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"

#
# 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

WindyCityTech Blogger
WindyWindyCityTech WordPress

Compiling aircrack-ng suite on Raspberry Pi Raspbian

Why, because you can.  I’m doing this on the stock upgraded Raspbian distro kernel 3.6.11+.

Prerequisites are a brain, knowledge of Linux and Raspberry Pi.  Casual tech self-abusers should go and do some research first and yes the Pi is helping them come out the woodwork.

Compiling from the source down not work, it has not been upgraded for a while.  Just check out the SVN.

sudo apt-get upgrade
sudo apt-get update

sudo apt-get install libssl-dev subversion iw
svn co http://trac.aircrack-ng.org/svn/trunk aircrack-ng
cd aircrack-ng
make
sudo make install

airmon-ng is no longer compiled during make by the looks of it, but it is in aircrack-ng/scripts

You can make it executable by doing a chmod +x airmon-ng, then copying it to the same path where aircrack-ng is natively installed.  i.e
/usr/local/bin/

To get airodump-ng to work properly, you may need to kill all the services that airmon-ng complains about then it is running.
Go to /etc/init.d and
sudo ./ifplugd stop

then sudo killall all the rest of the processes that airmon-ng complains about

PID    Name
1589    ifplugd
1617    ifplugd
2380    dhclient
16076    ifplugd
16088    wpa_supplicant
16146    wpa_cli
Process with PID 16076 (ifplugd) is running on interface wlan0
Process with PID 16088 (wpa_supplicant) is running on interface wlan0
Process with PID 16146 (wpa_cli) is running on interface wlan0

Seems to work ok

pi@raspberrypi /etc/init.d $ sudo aireplay-ng -9 mon0
00:03:07  Trying broadcast probe requests…
00:03:07  Injection is working!
00:03:09  Found 2 APs

00:03:09  Trying directed probe requests…
00:03:09  ########### – channel: 6 – ‘#########’
00:03:10  Ping (min/avg/max): 1.637ms/22.619ms/103.341ms Power: -51.40
00:03:10  30/30: 100%

00:03:10  ########## – channel: 6 – ‘#####’
00:03:11  Ping (min/avg/max): 2.875ms/32.890ms/87.744ms Power: -54.83
00:03:11  30/30: 100%

WindyCityTech Blogger
WindyWindyCityTech WordPress

Getting the maximum power and channels on the RTL8187 WLAN wifi card

Reference: http://ubuntuforums.org/archive/index.php/t-1814516.html

Because of your local areas regulations, the operating systems will limit the power and channels based your location rules.

Some locations in the world have no rules on power and channels.

If you want maximum power and channels do this:

sudo iw reg set BO
sudo iwconfig wlan1 txpower 30

Remember to heatsink the RTL8187 so it does not get fried by the power increase.

To check if the change was successful:
sudo iw reg get

You should see the BO region set.

WindyCityTech Blogger
WindyWindyCityTech WordPress

Installing Reaver WPS on Ubuntu 11 code.google.com/p/reaver-wps

What a great program.

To install on Ubuntu, you need to have the prerequisites:

  • a brain
  • build-essential  i.e apt-get install build-essential
  • libpcap0.8 library and the development library libpcap0.8-dev
  • aircrack-ng- preferably the subversion
  • patched compat-wireless drivers – no channel -1 shit
  • wireless card that can do injection
  • reaver wps code http://code.google.com/p/reaver-wps/

The rest is pretty simple, just the usual extraction., ./configure, make, sudo make install.
Pay attention to the ./configure and make processes, if there is anything missing required, check or investigate.

You will need to put you wireless card into monitor mode, use airmon-ng for this.

You will also need to establish a good, solid signal between the AP and you radio, if you cannot then it will wont work very quickly or at all.

WindyCityTech Blogger
WindyWindyCityTech WordPress

Installing Pyrit on Ubuntu 11.04

You could install pyrit using synaptic, but is usually a older version.

The best way to install the latest version is to download the source and compile it yourself.

But there is a problem satisfying all the dependencies, but we can cheat here.

  • Install pyrit using synaptic, version 0.3.0.  Leave it installed on your system.
  • Download the latest source from the pyrit website
  • Try and compile it python setup.py build if it has a spaz make sure all the dependencies are installed.

    I needed to do sudo apt-get install libpcap0.8 libpcap0.8-dev
    and also check g++ python-dev zlib1g-dev libssl-dev python-scapy while u are there.

  •  Once the dependencies are installed sudo python setup.py install

WindyCityTech Blogger
windywindycitytech wordpress

Installing Pyrit on Ubuntu 11.04

You could install pyrit using synaptic, but is usually a older version.

The best way to install the latest version is to download the source and compile it yourself.

But there is a problem satisfying all the dependencies, but we can cheat here.

  • Install pyrit using synaptic, version 0.3.0.  Leave it installed on your system.
  • Download the latest source from the pyrit website
  • Try and compile it python setup.py build if it has a spaz make sure all the dependencies are installed.

    I needed to do sudo apt-get install libpcap0.8 libpcap0.8-dev
    and also check g++ python-dev zlib1g-dev libssl-dev python-scapy while u are there.

  •  Once the dependencies are installed sudo python setup.py install

WindyCityTech Blogger
windywindycitytech wordpress