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

Quick install of Linux firmware htc_9271.fw for TP-LINK TLWN422G USB Wireless

Download firmware from here or search for newer.
http://wireless.kernel.org/download/htc_fw/1.3/htc_9271.fw

sudo cp htc_9271.fw /lib/firmware/.

Plug in device and do a dmesg to see if firmware is loaded.

Works great on airodump-ng, injection is super.\

lsusb

Bus 001 Device 004: ID 0cf3:1006 Atheros Communications, Inc. TP-Link TL-WN322G v3 / TL-WN422G v2 802.11g [Atheros AR9271]

Don’t waste time trying to compile anything else.

WindyCityTech Blogger
WindyWindyCityTech WordPress

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

Disable NTP sending out requests on Raspberry PI Linux

As beautiful as NTP is, NTP chatter can pollute your network traffic.

Forget removing NTP services, that is too dramatic for the system.  A simple hack is to edit the /etc/ntp.conf file and remove the servers.

sudo nano /etc/ntp.conf

scroll down and hash out the lines like below:

# You do need to talk to an NTP server or two (or three).
#server ntp.your-provider.example

# pool.ntp.org maps to about 1000 low-stratum NTP servers.  Your server will
# pick a different set every time it starts up.  Please consider joining the
# pool: <http://www.pool.ntp.org/join.html&gt;
#server 0.debian.pool.ntp.org iburst
#server 1.debian.pool.ntp.org iburst
#server 2.debian.pool.ntp.org iburst
#server 3.debian.pool.ntp.org iburst

Control-X and yes to save

Restart NTP:
sudo /etc/init.d/ntp restart

Check syslog
cat /var/log/syslog

WindyCityTech Blogger
WindyWindyCityTech WordPress

Compiling mdk3 on Raspberry Pi Raspberian for a wifi hell

Compiling mdk3 on Raspberry Pi Raspberian.  Tested on the on the stock upgraded Raspbian distro kernel 3.6.11+.

After you get aircrack-ng suite installed and working, this is a no-brainer.

wget http://homepages.tu-darmstadt.de/~p_larbig/wlan/mdk3-v6.tar.bz2

extract
tar -xvjf mdk3-v6.tar.bz2

cd mdk3-v6

make

then move the mdk3 file somewhere where the shell can pick it up like
/usr/local/bin/

 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