Monday, September 13, 2010

Wircon-0.0.2-1

I changed around the wireless-connection script. I created a status file, where the script would echo its status to (connected, disconnected, or down). I use this status on my conky bar. If the status is connected or disconnected, the script is running; if it's down, the script isn't. I also wrote in some traps do echo the 'down' status before the script exits. The last change is that it no longer exits after five attempts to connect, it keeps trying. Anyway, here it is:

#!/bin/sh

dhcpcdPID="/var/run/dhcpcd.pid"
networks=`cat /home/jason/devel/scripts/wircon/pref_networks.lst`
statfile="/home/jason/devel/scripts/wircon/status"
silent=1

# make sure the script is being run as root
if [[ $(id -u) != "0" ]]; then
    echo "This script must be run as root"
    exit 1
fi

# scan for essids located in pref_networks.lst
find_network() {
  for i in $networks; do
    iwlist wlan0 scan | grep $i &> /dev/null
    if [[ "$?" = "0" ]]; then
      local essid=$i
      break
    fi
  done

  case $essid in
    "SouthPacific")
    network="sp"
    ;;
    "SouthPacific_Extender")
    network="spx"
    ;;
    "Belkin_G_Wireless_C973D1")
    network="seans"
  esac
}

# if dhcpcd is running, stop it
do_dhcpcd_check() {
  if [[ -f $dhcpcdPID ]]; then
    if [[ $silent = "1" ]]; then
      dhcpcd -x
    else
      dhcpcd -x -q
    fi
  fi

}

# load wireless firmware
do_load_firmware() {
  [[ $silent = "1" ]] && echo "loading wireless firmware"
  ifconfig wlan0 up
}

# set essid and, if applicable, network key
do_set_network() {
  [[ $silent = "1" ]] && echo "setting network parameters"
  iwconfig wlan0 essid $ESSID
  if [[ $KEY != "" ]]; then
      iwconfig wlan0 key $KEY
  fi
}

# unset the network for graceful exit
do_unset_network() {
  do_dhcpcd_check
  ifconfig wlan0 down
}

# try for dhcp lease
do_dhcpcd() {
  if [[ $silent = "1" ]]; then
    dhcpcd
  else
    dhcpcd -q
  fi
}

# something that helps find a carrier
do_channel_auto() {
  [[ $silent = "1" ]] && echo "connecting to carrier"
  iwconfig wlan0 channel auto
}

# make sure it worked
check_connection() {
  ping -c 1 www.google.com &> /dev/null

  if [[ $? = "0" ]]; then
    return 0
  else
    return 1
  fi
}

# set the desired network
set_network() {
  case $network in
    "sp")
    ESSID="SouthPacific"
    KEY="6bcebdfbea3caf9f3cd36b31b8"
    ;;
    "spx")
    ESSID="SouthPacific_Extender"
    KEY="6bcebdfbea3caf9f3cd36b31b8"
    ;;
    "seans")
    ESSID="Belkin_G_Wireless_C973D1"
    KEY=
    ;;
    *)
    [[ $silent = "1" ]] && echo "$1 is not a recognized essid"
  esac
}

connection_monitor() {
  while [[ 1 = 1 ]]; do
    sleep 10
    check_connection
    if [[ $? = 1 ]]; then
      echo "disconnected" > $statfile
      main
    fi
    echo "connected" > $statfile
  done
}

# call all functions in proper order to establish connection
main() {
  trap 'echo "down" > $statfile; do_unset_network; exit;' INT
  trap 'echo "down" > $statfile; do_unset_network; exit;' TERM

  while [[ 1 = 1 ]]; do
    do_dhcpcd_check
    do_load_firmware
    do_set_network
    sleep 2
    do_dhcpcd
    do_channel_auto
    check_connection
    if [[ $? = 0 ]]; then
      [[ $silent = "1" ]] && echo "connection established"
      connection_monitor
    fi
  done
}

# check scripts usage
if [[ $# > 0 ]]; then
  if [[ $1 = "-s" ]]; then
    silent=0
    shift
  fi
  if [[ ! -z $1 ]]; then
    network=$1
  else
    do_load_firmware
    find_network
  fi
else
  do_load_firmware
  find_network
fi

set_network

main

exit 2

No comments:

Post a Comment