#!/bin/bash # sweetnsourbkr's wifi script for Atheros ver. 0.1 # This script enables wifi for my laptop outside the home network # Run this script as root only # Error codes: # 0: network has been acquired # 100: must run script as root # 101: network connection failed # 102: user chose not to connect # 103: syntax error # Check to see if the user is root if [[ "$(id -u)" != "0" ]] then echo "You must run this as root!" ; exit 100 ; fi # In case there's no essid or key, asume it's a public network if [[ $2 == "" ]] then essid=any ; else essid=$2 ; # Assign specified essid fi if [[ $3 == "" ]] then key=off ; else key=$3 ; # Assign specified WEP key fi # This function brings the interface online if_up() { # Bring the interface up ifconfig $device up # Notify the user what network he/she is using if [[ $essid == "any" ]] then echo "Scanning for networks ..." ; iwlist $device scan |more ; # List available networks else echo "Using essid $essid" ; fi # Notify the user what key he/she is using if [[ $key != "off" ]] then echo "Using new key $key" ; fi # Ask to connect echo "Attempt connection (y/n)?" read response # If chosen to connect, go to the next function # If chosen to choose a specific essid, exit the script and use # the additional arguments case $response in 'y') dhcp_get ;; 'Y') dhcp_get ;; *) if_down ;; esac } # Function to configure ath0 and obtain a DHCP IP address dhcp_get() { echo "Configuring interface ..."; iwconfig $device essid $essid key $key ; echo "Acquiring IP Address ... "; dhcpcd -d -t 10 $device; # The following will check if there was an IP addr issued if [[ -f /etc/dhcpc/dhcpcd-ath0.pid ]] then echo "Connection ready!"; echo ; iwconfig $device ; ifconfig $device ; exit 0 ; else echo "Connection attempt unsuccessful."; echo ; iwconfig $device ; # dhcpcd will automatically bring ath0 down exit 101 ; fi } # If chosen not to connect, turn off the interface if_down() { ifconfig $device down ; echo "WiFi interface not configured." ; exit 102 ; } # Initial greeting case $1 in 'ath0') device="$1" if_up ;; *) echo "Usage $0 ath0 [essid] [key]" exit 103 ;; esac