Skip to content

Instantly share code, notes, and snippets.

@Hardolaf
Created August 30, 2012 20:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Hardolaf/3540153 to your computer and use it in GitHub Desktop.
Save Hardolaf/3540153 to your computer and use it in GitHub Desktop.
#!/bin/sh
# This is a stripped-down script to connect to wpa networks on GNU/Linux systems
echo "* Connecting to the internet... "
if ps ax|awk '{print$5}'|grep -q 'wpa_supplicant'
then
echo -n " - Killing previous wpa_supplicant... "
if pkill wpa_supplicant; then
echo "done."
else
echo "FAILED!"
exit 1
fi
fi
if ps ax|awk '{print$5}'|grep -q 'dhclient'
then
echo -n " - Killing previous dhclient... "
if pkill dhclient; then
echo "done."
else
echo "FAILED!"
exit 1
fi
fi
if ps ax|awk '{print$5}'|grep -q 'dhcpcd'
then
echo -n " - Killing previous dhcpcd... "
if pkill dhclient; then
echo "done."
else
echo "FAILED!"
exit 1
fi
fi
echo -n " - Bringing up wireless interface... "
WIRELESS_DEVICE=$(iwconfig 2>/dev/nulll | awk '/^wlan/{print$1;exit}')
ifconfig $WIRELESS_DEVICE up
sleep 1s
echo "done."
echo -n " - Detecting which SSID to connect to... "
SSIDS="osuwireless"
for POSSIBLE_SSID in $SSIDS; do
if iwlist $WIRELESS_DEVICE scanning | grep "^\s*ESSID:\"$POSSIBLE_SSID\"" 1>/dev/null; then
SSID=$POSSIBLE_SSID
fi
done
if [ -z $SSID ]; then
echo "\nNo recognized network found, aborting."
return 1
else
echo $SSID
fi
echo -n " - Starting wpa_supplicant... "
if [ -e /etc/wpa_supplicant/wpa_supplicant.log ]; then
rm /etc/wpa_supplicant/wpa_supplicant.log
touch /etc/wpa_supplicant/wpa_supplicant.log
fi
WPA_SUPPLICANT_CONF=/etc/wpa_supplicant.conf
wpa_supplicant -Dwext -i$WIRELESS_DEVICE -c$WPA_SUPPLICANT_CONF -B -f/etc/wpa_supplicant/wpa_supplicant.log
echo "done."
echo -n " - Waiting for association with access point"
while ! grep '^Associated with [0-9a-f][0-9a-f]:[0-9a-f][0-9a-f]:[0-9a-f][0-9a-f]:[0-9a-f][0-9a-f]:[0-9a-f][0-9a-f]:[0-9a-f][0-9a-f]' 1>/dev/null /etc/wpa_supplicant/wpa_supplicant.log; do
echo -n "."
sleep .2s
done
echo " done."
echo -n " - Getting IP address..."
if which dhclient >/dev/null
then
if dhclient $WIRELESS_DEVICE $1
then
echo "done."
else
echo "FAILED!"
exit 1
fi
elif which dhcpcd >/dev/null
then
if dhcpcd $WIRELESS_DEVICE $1
then
echo "done."
else
echo "FAILED!"
exit 1
fi
else
echo "Unable to find dhcp client program (such as dhclient or dhcpcd), aborting"
fi
echo "+ Connected."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment