7
Jun
Posted by Derek@TheDailyLinux in Scripting » Add Comment »
Script to Wait for Wireless AP Association During System Startup
The following is a script that could prove to be useful in an embedded Linux environment utilizing a wireless adapter. In order to connect to a wireless network, the wireless adapter needs to associate with an AP which can take some time occasionally. If you’ve edited the /etc/network/interfaces file to automatically obtain an IP address via DHCP, and it doesn’t seem to be getting an IP address during system startup, then this script might be able to help. This script is intended to run on system startup and wait for the access point association. If one is not found, it will eventually timeout.
#!/bin/bash
#
# CONNECT TO WIRELESS NETWORK
# Author: Errol E. Burrow II <eburrow@gmail.com>
#
# call this script from /etc/rc.local
# this script uses fping which can be installed
# with sudo apt-get install fping
#
# globals
MYIP="192.168.0.121"
WAPNAME="SSVR3"
GATEWAY="192.168.0.141"
PINGTESTIP="10.10.10.1"
RETRYCOUNT=10
# turn on extra regex features
shopt -s extglob
until [ $RETRYCOUNT -lt 1 ]; do
ifconfig wlan0 $MYIP
iwconfig wlan0 essid $WAPNAME
# get the access point mac address for wlan0
ap=$(iwconfig wlan0 | sed "s/Access/~+&/" | tr "~" "\n" | grep "+" | cut -c16- | tr -d " ")
echo "access point mac: [$ap]"
# check if the ap actually has an expected value
if [[ $ap =~ [0-9a-f]{2}[:-] ]]; then
echo "success: connected to wifi access point!"
route add default gw $GATEWAY
echo "added default gateway $GATEWAY"
echo "testing ping to $PINGTESTIP"
if fping -a -r1 $PINGTESTIP > /dev/null
then
echo "success: $PINGTESTIP is alive!"
break
else
echo "warning: $PINGTESTIP cannot be reached!"
fi
else
echo "warning: not connected to wifi access point"
# try again
fi
echo "retrying connection: $RETRYCOUNT"
let RETRYCOUNT-=1
# sleep a bit
sleep 1
done
Feel free to donate if this post prevented any headaches! Another way to show your appreciation is to take a gander at these relative ads that you may be interested in:
Here are some similar posts that you may be interested in:
There's 0 Comment So Far
Share your thoughts, leave a comment!