Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- # /usr/local/bin/ReconnectNetwork
- # (c) Kevin Trumbull 2010-11-29
- # --------------- Notes ---------------
- # This is a standalone script. It does not interact with or depend on any other script or framework.
- # It has been tested on MacOS X 10.5 and 10.6.
- # It requires an appropriate LaunchD script to make it run (every 60 seconds by design)
- #
- # On a desktop machine, the script will not reconnect to Wifi if the wired ethernet card is plugged in, active, and has an IP.
- #
- # In order to make the script not run on a laptop, Airport must be disabled in the control panel.
- # --------------- Variables ----------------
- # Command path
- PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/bin
- # File Locations
- LOGFILE="/var/log/PUHSD/connectivity.log"
- LOCKFILE="/var/log/PUHSD/lock/networklocation.lock"
- BOOTLOG="/var/log/PUHSD/boot.log"
- SLEEPLOG="/var/log/PUHSD/sleep.log"
- WAKELOG="/var/log/PUHSD/wake.log"
- # Model of Macintosh
- MACTYPE=`sysctl -n hw.model | sed -e 's/[0-9]*,[0-9]*//'`
- # Ethernet (802.3)
- ETHERNETSTATUS=`ifconfig en0 | awk '/status:/{ print $NF }'`
- ETHERNETIP=`networksetup -getinfo Ethernet | awk '/IP address/{ print $NF }'`
- # Airport (802.11)
- AIRPORTIP=`networksetup -getinfo Airport | awk '/IP address/{ print $NF }'`
- AIRPORTENABLED=`networksetup -getnetworkserviceenabled Airport`
- # Network Locations (Profiles)
- NETWORKLOCATIONS=`scselect 2>&1 | cat | sed -ne '/[A-Z0-9]\{8\}-[A-Z0-9]\{4\}-[A-Z0-9]\{4\}-[A-Z0-9]\{4\}-[A-Z0-9]\{12\}/p' | wc -l | sed -e 's/\ //g'`
- ORIGINALNETWORKLOCATION="`scselect 2>&1 | cat | sed -ne '/^\ *\*/p' | awk '{ print $NF }' | sed -e 's/[()]//g'`"
- ALTERNATENETWORKLOCATION="`scselect 2>&1 | cat | sed -ne '/^\ \ /p' | awk '{ print $NF }' | sed -e 's/[()]//g' | head -n 1`"
- # Temporal Settings
- TIME=`date +"%Y.%m.%d-%H.%M"`
- STARTTIME=`sysctl kern.boottime | grep -Eo "\ sec\ =\ [0-9]*" | awk '{print $NF }'`
- SLEEPTIME=`sysctl kern.sleeptime | grep -Eio "\ sec\ =\ [0-9]*" | awk '{print $NF }'`
- WAKETIME`sysctl kern.waketime | grep -Eo "\ sec\ =\ [0-9]*" | awk '{print $NF}'`
- NOW=`date +%s`
- # Various other settings
- USER="AirportHelper"
- MACHINENAME=`hostname -s | tr [:lower:] [:upper:]`
- # --------------- Main Script ---------------
- touch $LOGFILE
- touch $BOOTLOG
- touch $SLEEPLOG
- touch $WAKELOG
- # ----- Log Boot/Sleep/Wake Times -----
- # Only one entry per Boot
- if [ "`tail -1 $BOOTLOG | awk -F "\t" { print $2 }`" -ne "$STARTTIME" ]; then
- printf "$STARTTIME\t`date -r $STARTTIME`\n" >> $BOOTLOG
- fi
- if [ "`tail -1 $SLEEPLOG | awk -F "\t" { print $2 }`" -ne "$SLEEPTIME" ]; then
- printf "$SLEEPTIME\t`date -r $SLEEPTIME`\n" >> $SLEEPLOG
- fi
- if [ "`tail -1 $WAKELOG | awk -F "\t" { print $2 }`" -ne "$WAKETIME" ]; then
- printf "$WAKETIME\t`date -r $WAKETIME`\n" >> $WAKELOG
- fi
- # ----- Network Reset Section -----
- # If a lockfile exists and it's older than 5 minutes; delete it
- if [ -e $LOCKFILE ]; then
- LOCKFILECREATED=`stat -f "%B" $LOCKFILE`
- if [ "$(($NOW-$LOCKFILECREATED))" -gt "300" ]; then
- rm $LOCKFILE
- fi
- fi
- # Determine what kind of machine this is running on (Variable set for legibility)
- if [ "$MACTYPE" == "MacBook" ] || [ "$MACTYPE" == "MacBookPro" ] || [ "$MACTYPE" == "MacBookAir" ] || [ "$MACTYPE" == "PowerBook" ] ||[ "$MACTYPE" == "iBook" ]; then
- FORMFACTOR="Laptop"
- elif [ "$MACTYPE" == "MacPro" ] || [ "$MACTYPE" == "iMac" ] || [ "$MACTYPE" == "Macmini" ] || [ "$MACTYPE" == "PowerMac" ]; then
- FORMFACTOR="Desktop"
- elif [ "$MACTYPE" == "RackMac" ] || [ "$MACTYPE" == "Xserve" ]; then
- FORMFACTOR="Server"
- # I'm not sure why you'd have a server using Wifi, but...
- elif [ "$MACTYPE" == "AppleTV" ] || [ "$MACTYPE" == "iPod" ] || [ "$MACTYPE" == "iPad" ] || [ "$MACTYPE" == "iPhone" ]; then
- FORMFACTOR="iDevice"
- # Seriously???
- # Does the script work?
- else
- FORMFACTOR="Unknown"
- fi
- # If the machine is a desktop whose ethernet cable is plugged in...
- if [ "$FORMFACTOR" == "Desktop" ] && [ "$ETHERNETSTATUS" == "active" ]
- # ...and which has an IP address, disable reconnection attempt
- if [ "$ETHERNETIP" != "" ] || [ "`echo $ETHERNETIP | sed -e '/^169.*/d'`" != "" ]; then
- exit 0
- fi
- fi
- # Make sure there is no lockfile
- if [ ! -e $LOCKFILE ]; then
- # If Wifi is disabled, disable reconnection attempt
- if [ "$AIRPORTENABLED" == "Disabled" ]; then
- exit 0
- fi
- # If machine has been on for less than 4 minutes, disable reconnection attempt
- if [ "$(($NOW-$STARTTIME))" -gt "240" ]; then
- exit 0
- fi
- # If machine has been awake less than 2 minutes, disable reconnection attempt
- if [ "$(($NOW-$WAKETIME))" -lt "120" ]; then
- exit 0
- fi
- # If reset conditions are met and Airport has no IP address, reset wifi connection
- if [ "$AIRPORTIP" == "" ] || [ "`echo $AIRPORTIP | sed -e '/^169.*/d'`" == "" ]; then
- # Create lockfile
- touch $LOCKFILE
- # Multiple network locations exist; switch to alternate network location and cycle the power on the WiFi card
- if [ "$NETWORKLOCATIONS" -gt "1" ]; then
- scselect "$ALTERNATENETWORKLOCATION"
- sleep 5
- networksetup -setairportpower off
- sleep 5
- networksetup -setairportpower on
- sleep 50
- # Wifi is still offline; log this, switch back to original network location, and cycle the power on the WiFi card
- if [ "$AIRPORTIP" == "" ] || [ "`echo $AIRPORTIP | sed -e '/^169.*/d'`" == "" ]; then
- printf "$TIME\t$USER\t$MACHINENAME\tWarning\tNo valid Wifi IP address for 3 minutes - Switching to Original Configuration\n" >> $LOGFILE
- scselect "$ORIGINALNETWORKLOCATION"
- sleep 5
- networksetup -setairportpower off
- sleep 5
- networksetup -setairportpower on
- sleep 60
- # If WiFi is still offline, log failure - no more reconnection attempts for now
- if [ "$AIRPORTIP" == "" ] || [ "`echo $AIRPORTIP | sed -e '/^169.*/d'`" == "" ]; then
- printf "$TIME\t$USER\t$MACHINENAME\tFailure\tWifi reconnection failed - Perhaps signal strength is low" >> $LOGFILE
- exit 1
- # If Wifi is online after switching back to "original" log this
- else
- printf "$TIME\t$USER\t$MACHINENAME\tWarning\tOriginal Configuration is working\n" >> $LOGFILE
- exit 0
- fi
- # Wifi is online after switching to "alternate" log this
- else
- printf "$TIME\t$USER\t$MACHINENAME\tWarning\tAlternate Configuration is working\n" >> $LOGFILE
- exit 0
- fi
- # Only one network location exists, cycle Airport power and hope that works
- else
- networksetup -setairportpower off
- sleep 30
- networksetup -setairportpower on
- sleep 50
- if [ "$AIRPORTIP" == "" ] || [ "`echo $AIRPORTIP | sed -e '/^169.*/d'`" == "" ]; then
- printf "$TIME\t$USER\t$MACHINENAME\tFailure\tWifi connection still down - Add another network location for better results" >> $LOGFILE
- exit 1
- fi
- fi
- fi
- fi
- exit 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement