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

[Solved] Installing aircrack-ng on Dell Inspiron Mini 10 (1012) Netbook b43

Fuck Broadcom propriety shit!

Scenario:
aircrack-ng wont work out the box on a stock ubuntu 11.04 install, that is because it is using the wl driver.

  1. Install aircrack-ng suite
  2. Disable wl driver
  3. Remove bcmwl
  4. Install compat-wireles & patches.

Checkout and compile the latest svn of aircrack-ng
sudo apt-get install svn
svn co http://trac.aircrack-ng.org/svn/trunk/ aircrack-ng

sudo apt-get install iw

Check wl is blacklisted
cd /etc/modprobe.d
cat * | grep wl

Enter synaptic, Completly remove bcmwl
You may notice nm-applet is saying “firmware no loaded” if you try to connect with wireless – good

I followed instructions on the aircrack-ng site to install compat-wireless and patch for aircrack, then install Broadcom firmware BS!
source: http://aircrack-ng.org/doku.php?id=compat-wireless

wget http://wireless.kernel.org/download/compat-wireless-2.6/compat-wireless-$(date -I).tar.bz2
tar -jxf compat-wireless-$(date -I).tar.bz2
cd compat-wireless-$(date -I)
wget http://patches.aircrack-ng.org/mac80211.compat08082009.wl_frag+ack_v1.patch
wget http://patches.aircrack-ng.org/channel-negative-one-maxim.patch
patch -p1 < mac80211.compat08082009.wl_frag+ack_v1.patch
patch -p1 < channel-negative-one-maxim.patch
./scripts/driver-select b43
make
sudo make install
sudo apt-get install b43-fwcutter firmware-b43-installer
sudo modprobe -r b43 ssb
sudo modprobe b43

check it! (assume your wlan is wlan0)
sudo airmon-ng start wlan0
sudo aireplay-ng -9 mon0

Now make it work properly on boot (important).
ref: https://bugs.launchpad.net/ubuntu/+source/linux/+bug/184600/comments/34

To make ssb not load you have to blacklist it, reboot and execute

sudo update-initramfs -k all -u

If you fail to do this, ssb is loaded before b43 and it will cause error sand the b43 module will fail to load correctly.  Lots of ssb and b43 errors in your dmesg.

Remember to unblacklist ssb after the update-initramfs.

If you still have this problem, I suggest that you make a script in your init.d


#!/bin/sh
modprobe -r ssb
modprobe -r b43
modprobe b43
modprobe ssb

WindyCityTech Blogger
windywindycitytech wordpress

[Solved] Installing aircrack-ng on Dell Inspiron Mini 10 (1012) Netbook b43

Fuck Broadcom propriety shit!

Scenario:
aircrack-ng wont work out the box on a stock ubuntu 11.04 install, that is because it is using the wl driver.

  1. Install aircrack-ng suite
  2. Disable wl driver
  3. Remove bcmwl
  4. Install compat-wireles & patches.

Checkout and compile the latest svn of aircrack-ng
sudo apt-get install svn
svn co http://trac.aircrack-ng.org/svn/trunk/ aircrack-ng

sudo apt-get install iw

Check wl is blacklisted
cd /etc/modprobe.d
cat * | grep wl

Enter synaptic, Completly remove bcmwl
You may notice nm-applet is saying “firmware no loaded” if you try to connect with wireless – good

I followed instructions on the aircrack-ng site to install compat-wireless and patch for aircrack, then install Broadcom firmware BS!
source: http://aircrack-ng.org/doku.php?id=compat-wireless

wget http://wireless.kernel.org/download/compat-wireless-2.6/compat-wireless-$(date -I).tar.bz2
tar -jxf compat-wireless-$(date -I).tar.bz2
cd compat-wireless-$(date -I)
wget http://patches.aircrack-ng.org/mac80211.compat08082009.wl_frag+ack_v1.patch
wget http://patches.aircrack-ng.org/channel-negative-one-maxim.patch
patch -p1 < mac80211.compat08082009.wl_frag+ack_v1.patch
patch -p1 < channel-negative-one-maxim.patch
./scripts/driver-select b43
make
sudo make install
sudo apt-get install b43-fwcutter firmware-b43-installer
sudo modprobe -r b43 ssb
sudo modprobe b43

check it! (assume your wlan is wlan0)
sudo airmon-ng start wlan0
sudo aireplay-ng -9 mon0

Now make it work properly on boot (important).
ref: https://bugs.launchpad.net/ubuntu/+source/linux/+bug/184600/comments/34

To make ssb not load you have to blacklist it, reboot and execute

sudo update-initramfs -k all -u

If you fail to do this, ssb is loaded before b43 and it will cause error sand the b43 module will fail to load correctly.  Lots of ssb and b43 errors in your dmesg.

Remember to unblacklist ssb after the update-initramfs.

If you still have this problem, I suggest that you make a script in your init.d


#!/bin/sh
modprobe -r ssb
modprobe -r b43
modprobe b43
modprobe ssb

WindyCityTech Blogger
windywindycitytech wordpress

MSI L1350D Netbook Wireless Lan Chipset

I have been thinking avbout buying the MSI L1350D Netbook. The price at Officeworks in Australia is $249, which is a reasonable price in my opinion.

I went to a store to check it out and on initial imression it looks great.  The keyboard has nice raised keys, the hinges are good, it has becent ventilation and the DC power plug looks meaty (unlike some eeepc’s).

The wireless chipset on this one is a Ralink RT2860, i could see it though the ventilation slots.

It has Realtek sound card as well.

Should be good with Aircrack-ng, rf mon seems to work with what I have seen on the net.

The only thing that looks yuck on it is the VGA connector, would be nice if it was a HDMI.

MSI L1350D Netbook Wireless Lan Chipset

I have been thinking avbout buying the MSI L1350D Netbook. The price at Officeworks in Australia is $249, which is a reasonable price in my opinion.

I went to a store to check it out and on initial imression it looks great.  The keyboard has nice raised keys, the hinges are good, it has becent ventilation and the DC power plug looks meaty (unlike some eeepc’s).

The wireless chipset on this one is a Ralink RT2860, i could see it though the ventilation slots.

It has Realtek sound card as well.

Should be good with Aircrack-ng, rf mon seems to work with what I have seen on the net.

The only thing that looks yuck on it is the VGA connector, would be nice if it was a HDMI.