Tuesday, October 26, 2010

[update]wircon

Fixes:
If the find_network function failed, the script would still try to connect to a network, which would cause an error because there was no network set to connect to.
An instance where main was called from a function which was called by main. Now it just returns to main rather than calling it again.
An edit to the restart function in the init script was also necessary. The delay between stopping and starting the script was changed from 1 second to 2.

Still to fix:
If the script is stopped before it enters the 'main' function, the signal wont be caught and the do_unset_network function wont be run.

#!/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
# chkroot && [[ $? = 0 ]] || exit 1

echo "starting" > $statfile

##################################################################
# 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"
            ;;
        "NETGEAR-Go")
            network="seans2"s
            ;;
        "MadisonOaks")
            network="daniels"
            ;;
        *)
            network=
  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_params() {
  case $network in
    "sp")
            ESSID="SouthPacific"
            KEY="6bcebdfbea3caf9f3cd36b31b8"
            ;;
    "spx")
            ESSID="SouthPacific_Extender"
            KEY="6bcebdfbea3caf9f3cd36b31b8"
            ;;
    "seans")
            ESSID="Belkin_G_Wireless_C973D1"
            KEY=
            ;;
        "seans2")
            ESSID="NETGEAR-Go"
            KEY=
            ;;
        "daniels")
            ESSID="MadisonOaks"
            KEY=
            ;;
    *)
            return 1
  esac

    return 0
}

##################################################################
# make sure we're still connected to the network
connection_monitor() {
  while [[ 1 = 1 ]]; do
    sleep 10
    check_connection
    if [[ $? = 1 ]]; then
      echo "disconnected" > $statfile
      return
    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
        set_network_params
        [[ $? = 0 ]] && break
        echo "searching" > $statfile
        find_network
    done

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


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

main

exit 2

No comments:

Post a Comment