Up one level
A silly little script for Linux wireless networking
Spencer Stirling

This is NOT a Linux wireless howto. If you want that, check into getting the right drivers compiled into your kernel, and look into the "wireless-tools" Debian package. Wireless can be rather easy, or it can be a complete pain. The kernel is constantly improving, however - so keep sharp. Here I am listing the contents of a simple shell script that I wrote to automate establishing a wireless connection.

There are many tools for doing this (e.g. KWifiManager), but for some reason they don't work reliably for me. Probably your experience will be different. I've given up with the GUI stuff and have gone back to good old command line.

Tedious commands
Whenever I plug in my wireless card, I find that (as root) I need to run the following commands. First, bring up the card (assuming that the kernel has assigned the card to interface "ath0")

ifconfig ath0 up
Then, check what's out there
iwlist ath0 scanning
Next, give the card the right parameters
iwconfig ath0 essid [YOURWIRELESSGATEWAY]
iwconfig ath0 rate 54M
iwconfig ath0 key 8493023948
Obviously, the last command is not necessary if the gateway is not encrypted. Next, it's time to get an IP address using DHCP
dhclient ath0
Finally, I have a firewall script that I run
firewall.sh ath0

Here's a very simple script that automates these tasks. Obviously, this script needs to be run using sudo if a regular user invokes it, like so:

sudo wireless
Of course, the user must be given sudo access to the script - try my shutdown article for an example to see how "sudo" can allow a regular user to run programs "as root".

#!/bin/sh
# syntax
# if run without arguments, will prompt
# if run with arguments, you need
# wireless [essid] [rate] [WEPkey]
#  WEPkey can be "none" if you don't want to be prompted

INTERFACE="ath0"
IFCONFIG="/sbin/ifconfig"
IWLIST="/sbin/iwlist"
IWCONFIG="/sbin/iwconfig"
DHCLIENT="/sbin/dhclient"
FIREWALL="/usr/bin/firewall.sh"

# first just bring up the interface
$IFCONFIG $INTERFACE up 2> /dev/null
if [ $? -ne 0 ]
then
  echo "Cannot execute: $IFCONFIG $INTERFACE up"
  exit
fi  


# get the essid
if [ "$1" = "" ]
then

  # ask if the user wants a list
  echo -n "Would you like a list? [y]"
  read PROMPT
  case "$PROMPT" in
    [yY]	) $IWLIST $INTERFACE scanning | more;;
    ""	) $IWLIST $INTERFACE scanning | more;;
    *     ) echo "OK, find it yourself then";;
  esac

  # prompt for the rest
  echo -n "ESSID (name?):"
  read ESSID
else
  ESSID="$1"
fi

# get the rate
if [ "$2" = "" ]
then
  echo -n "rate [54M]:"
  read RATE
  if [ "$RATE" = "" ]
  then
    RATE="54M"
  fi  
else
  RATE="$2"
fi

# get the WEPkey
if [ "$3" = "" ]
then
  echo -n "WEPkey [none]:"
  read WEP
else
  WEP="$3"
  if [ "$WEP" = "none" ]
  then
    WEP=""
  fi
fi

# start running the commands NOW

$IWCONFIG $INTERFACE essid "$ESSID" 2> /dev/null
if [ $? -ne 0 ]
then
  echo "Cannot $IWCONFIG $INTERFACE essid $ESSID"
  exit
fi  

$IWCONFIG $INTERFACE rate "$RATE" 2> /dev/null
if [ $? -ne 0 ]
then
  echo "Cannot $IWCONFIG $INTERFACE rate $RATE"
  exit
fi  

# now set the key, if necessary
if [ "$WEP" != "" ]
then
  $IWCONFIG $INTERFACE key "$WEP" 2> /dev/null
  if [ $? -ne 0 ]
  then
    echo "Cannot $IWCONFIG $INTERFACE key $WEP"
    exit
  fi  
fi  

$DHCLIENT $INTERFACE 2> /dev/null
if [ $? -ne 0 ]
then
  echo "Cannot $DHCLIENT $INTERFACE"
  exit
fi  

$FIREWALL $INTERFACE

This page has been visited   times since April 28, 2006