#!/bin/sh # # Automatically try to connect an FreeBSD Client to Wireless Leiden Network # and or maintain connection status. # # Tip: Place it in crontab like this: # */5 * * * * root /usr/local/sbin/wl-auto-connect --auto 2>&1 | /usr/bin/logger -t wl-auto-connect # # Rick van der Zwet # WIRELESS_IF=wlan0 WIRED_IF=`ifconfig -l | tr ' ' '\n' | grep -v -e lo0 -e bridge0 -e ath0 -e wlan0 | head -1` SSID_FILTER='-e ^ap-WirelessLeiden' WAIT_DELAY=10 # This function is special to WirelessLeiden Announcement, try to find your own # announcement method for any other use announce_client() { echo "# INFO: Announcing Client available" if ! `fetch -q http://dyndns.wleiden.net/dyndns/update/$(hostname -s)/`; then echo "# WARN: Hostname announcement failed!" exit 2 fi } connect_client() { # Do we need to connect to the Wired Interface if `ifconfig $WIRED_IF | grep -q 'status: active'`; then if `dhclient $WIRED_IF`; then announce_client exit 0 else echo "# ERROR: $WIRED_IF has ethernet connection but DHCP not working" fi fi # Find out the SSIDs to connect to AVAILABLE_SSIDS=`ifconfig -v $WIRELESS_IF list scan | grep $SSID_FILTER | awk '{print $5 " " $1}' | sort -t: -k1 -nr | awk '{print $2}'` if [ -z "$AVAILABLE_SSIDS" ]; then echo "# ERROR: Cannot find an (Wireless Leiden) AccessPoints" exit 1 fi # Attempt to make an connection to one of the available SSIDs for AVAILABLE_SSID in $AVAILABLE_SSIDS; do echo "# INFO: Trying to connect with $WIRELESS_IF to $AVAILABLE_SSID" ifconfig $WIRELESS_IF ssid $AVAILABLE_SSID up echo "# INFO: Sleeping $WAIT_DELAY for interface $WIRELESS_IF to settle" sleep $WAIT_DELAY if `ifconfig $WIRELESS_IF | grep -q 'status: associated'`; then echo "# INFO: Connection established of $WIRELESS_IF to $AVAILABLE_SSID" if `dhclient $WIRELESS_IF`; then announce_client exit 0 fi else echo "# WARN: Connection of $WIRELESS_IF to $AVAILABLE_SSID failed!" fi done echo "# ERROR: Unable to connect to the network" exit 1 } if [ -z "$1" -o "$1" == "--help" ]; then echo "Usage: $0 [--help|--auto|--force]" 1>&2 echo " --help : Reading right now" 1>&2 echo " --auto : Attempt auto-configuration" 1>&2 echo " --force : Force configured setup to re-connect" 1>&2 exit 128 elif [ "$1" == "--force" ]; then connect_client elif [ "$1" == "--auto" ]; then # If we cannot ICMP ping the gateway we have to reconnect DEFAULTROUTE=`route get default | awk '/gateway:/ {print $2}'` if [ -n "$FORCE_UPDATE" ]; then connect_client elif [ -z "$DEFAULTROUTE" ]; then connect_client elif ! `ping -c 3 -t 1 $DEFAULTROUTE > /dev/null`; then connect_client else # Phone Home announce_client fi fi