Advertisement
KevinATrumbull

ReconnectNetwork

Oct 23rd, 2014
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 7.43 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. # /usr/local/bin/ReconnectNetwork
  4. # (c) Kevin Trumbull 2010-11-29
  5.  
  6. # --------------- Notes ---------------
  7. # This is a standalone script.  It does not interact with or depend on any other script or framework.
  8. # It has been tested on MacOS X 10.5 and 10.6.
  9. # It requires an appropriate LaunchD script to make it run (every 60 seconds by design)
  10. #
  11. # On a desktop machine, the script will not reconnect to Wifi if the wired ethernet card is plugged in, active, and has an IP.
  12. #
  13. # In order to make the script not run on a laptop, Airport must be disabled in the control panel.
  14.  
  15.  
  16. # --------------- Variables ----------------
  17.  
  18. # Command path
  19. PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/bin
  20. # File Locations
  21. LOGFILE="/var/log/PUHSD/connectivity.log"
  22. LOCKFILE="/var/log/PUHSD/lock/networklocation.lock"
  23. BOOTLOG="/var/log/PUHSD/boot.log"
  24. SLEEPLOG="/var/log/PUHSD/sleep.log"
  25. WAKELOG="/var/log/PUHSD/wake.log"
  26. # Model of Macintosh
  27. MACTYPE=`sysctl -n hw.model | sed -e 's/[0-9]*,[0-9]*//'`
  28. # Ethernet (802.3)
  29. ETHERNETSTATUS=`ifconfig en0 | awk '/status:/{ print $NF }'`
  30. ETHERNETIP=`networksetup -getinfo Ethernet | awk '/IP address/{ print $NF }'`
  31. # Airport (802.11)
  32. AIRPORTIP=`networksetup -getinfo Airport | awk '/IP address/{ print $NF }'`
  33. AIRPORTENABLED=`networksetup -getnetworkserviceenabled Airport`
  34. # Network Locations (Profiles)
  35. 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'`
  36. ORIGINALNETWORKLOCATION="`scselect 2>&1 | cat | sed -ne '/^\ *\*/p' | awk '{ print $NF }' | sed -e 's/[()]//g'`"
  37. ALTERNATENETWORKLOCATION="`scselect 2>&1 | cat | sed -ne '/^\ \ /p' | awk '{ print $NF }' | sed -e 's/[()]//g' | head -n 1`"
  38. # Temporal Settings
  39. TIME=`date +"%Y.%m.%d-%H.%M"`
  40. STARTTIME=`sysctl kern.boottime | grep -Eo "\ sec\ =\ [0-9]*" | awk '{print $NF }'`
  41. SLEEPTIME=`sysctl kern.sleeptime | grep -Eio "\ sec\ =\ [0-9]*" | awk '{print $NF }'`
  42. WAKETIME`sysctl kern.waketime | grep -Eo "\ sec\ =\ [0-9]*" | awk '{print $NF}'`
  43. NOW=`date +%s`
  44. # Various other settings
  45. USER="AirportHelper"
  46. MACHINENAME=`hostname -s | tr [:lower:] [:upper:]`
  47.  
  48. # --------------- Main Script ---------------
  49.  
  50. touch $LOGFILE
  51. touch $BOOTLOG
  52. touch $SLEEPLOG
  53. touch $WAKELOG
  54.  
  55. # ----- Log Boot/Sleep/Wake Times -----
  56. # Only one entry per Boot
  57. if [ "`tail -1 $BOOTLOG | awk -F "\t" { print $2 }`" -ne "$STARTTIME" ]; then
  58.     printf "$STARTTIME\t`date -r $STARTTIME`\n" >> $BOOTLOG
  59. fi
  60. if [ "`tail -1 $SLEEPLOG | awk -F "\t" { print $2 }`" -ne "$SLEEPTIME" ]; then
  61.     printf "$SLEEPTIME\t`date -r $SLEEPTIME`\n" >> $SLEEPLOG
  62. fi
  63. if [ "`tail -1 $WAKELOG | awk -F "\t" { print $2 }`" -ne "$WAKETIME" ]; then
  64.     printf "$WAKETIME\t`date -r $WAKETIME`\n" >> $WAKELOG
  65. fi
  66.  
  67.  
  68. # ----- Network Reset Section -----
  69. # If a lockfile exists and it's older than 5 minutes; delete it
  70. if [ -e $LOCKFILE ]; then
  71.      LOCKFILECREATED=`stat -f "%B" $LOCKFILE`
  72.      if [ "$(($NOW-$LOCKFILECREATED))" -gt "300" ]; then
  73.           rm $LOCKFILE
  74.      fi
  75. fi
  76.  
  77.  
  78. # Determine what kind of machine this is running on (Variable set for legibility)
  79. if [  "$MACTYPE" == "MacBook" ] || [ "$MACTYPE" == "MacBookPro" ] || [ "$MACTYPE" == "MacBookAir" ] || [ "$MACTYPE" == "PowerBook" ] ||[ "$MACTYPE" == "iBook"  ]; then
  80.      FORMFACTOR="Laptop"
  81. elif [ "$MACTYPE" == "MacPro" ] || [ "$MACTYPE" == "iMac" ] || [ "$MACTYPE" == "Macmini" ] || [ "$MACTYPE" == "PowerMac" ]; then
  82.     FORMFACTOR="Desktop"
  83. elif [ "$MACTYPE" == "RackMac" ] || [ "$MACTYPE" == "Xserve" ]; then
  84.     FORMFACTOR="Server"
  85.     # I'm not sure why you'd have a server using Wifi, but...
  86. elif [ "$MACTYPE" == "AppleTV" ] || [ "$MACTYPE" == "iPod" ] || [ "$MACTYPE" == "iPad" ] || [ "$MACTYPE" == "iPhone" ]; then
  87.     FORMFACTOR="iDevice"
  88.     # Seriously???
  89.     # Does the script work?
  90. else
  91.     FORMFACTOR="Unknown"
  92. fi
  93.  
  94. # If the machine is a desktop whose ethernet cable is plugged in...
  95. if [ "$FORMFACTOR" == "Desktop" ] && [ "$ETHERNETSTATUS" == "active" ]
  96.     # ...and which has an IP address, disable reconnection attempt
  97.     if [ "$ETHERNETIP" != "" ] || [ "`echo $ETHERNETIP | sed -e '/^169.*/d'`" != "" ]; then
  98.         exit 0
  99.     fi
  100. fi
  101.  
  102.  
  103.  
  104. # Make sure there is no lockfile
  105. if [ ! -e $LOCKFILE ]; then
  106.  
  107.      # If Wifi is disabled, disable reconnection attempt
  108.      if [ "$AIRPORTENABLED" == "Disabled" ]; then
  109.           exit 0
  110.      fi
  111.  
  112.      # If machine has been on for less than 4 minutes, disable reconnection attempt
  113.      if [ "$(($NOW-$STARTTIME))" -gt "240" ]; then
  114.           exit 0
  115.      fi
  116.  
  117.      # If machine has been awake less than 2 minutes, disable reconnection attempt
  118.      if [ "$(($NOW-$WAKETIME))" -lt "120" ]; then
  119.           exit 0
  120.      fi
  121.    
  122.      # If reset conditions are met and Airport has no IP address, reset wifi connection
  123.      if [ "$AIRPORTIP" == "" ] || [ "`echo $AIRPORTIP | sed -e '/^169.*/d'`" == "" ]; then
  124.           # Create lockfile
  125.           touch $LOCKFILE
  126.           # Multiple network locations exist; switch to alternate network location and cycle the power on the WiFi card
  127.           if [ "$NETWORKLOCATIONS" -gt "1" ]; then
  128.                scselect "$ALTERNATENETWORKLOCATION"
  129.                sleep 5
  130.                networksetup -setairportpower off
  131.                sleep 5
  132.                networksetup -setairportpower on
  133.                sleep 50
  134.                # Wifi is still offline; log this, switch back to original network location, and cycle the power on the WiFi card
  135.                if [ "$AIRPORTIP" == "" ] || [ "`echo $AIRPORTIP | sed -e '/^169.*/d'`" == "" ]; then
  136.                     printf "$TIME\t$USER\t$MACHINENAME\tWarning\tNo valid Wifi IP address for 3 minutes - Switching to Original Configuration\n" >> $LOGFILE
  137.                     scselect "$ORIGINALNETWORKLOCATION"
  138.                     sleep 5
  139.                     networksetup -setairportpower off
  140.                     sleep 5
  141.                     networksetup -setairportpower on
  142.                     sleep 60
  143.                     # If WiFi is still offline, log failure - no more reconnection attempts for now
  144.                     if [ "$AIRPORTIP" == "" ] || [ "`echo $AIRPORTIP | sed -e '/^169.*/d'`" == "" ]; then
  145.                          printf "$TIME\t$USER\t$MACHINENAME\tFailure\tWifi reconnection failed - Perhaps signal strength is low" >> $LOGFILE
  146.                          exit 1
  147.                     # If Wifi is online after switching back to "original" log this
  148.                     else
  149.                          printf "$TIME\t$USER\t$MACHINENAME\tWarning\tOriginal Configuration is working\n" >> $LOGFILE
  150.                          exit 0
  151.                     fi
  152.                # Wifi is online after switching to "alternate" log this
  153.                else
  154.                     printf "$TIME\t$USER\t$MACHINENAME\tWarning\tAlternate Configuration is working\n" >> $LOGFILE
  155.                     exit 0
  156.                fi
  157.              
  158.           # Only one network location exists, cycle Airport power and hope that works
  159.           else
  160.                networksetup -setairportpower off
  161.                sleep 30
  162.                networksetup -setairportpower on
  163.                sleep 50
  164.                if [ "$AIRPORTIP" == "" ] || [ "`echo $AIRPORTIP | sed -e '/^169.*/d'`" == "" ]; then
  165.                     printf "$TIME\t$USER\t$MACHINENAME\tFailure\tWifi connection still down - Add another network location for better results" >> $LOGFILE
  166.                     exit 1
  167.                fi
  168.           fi
  169.      fi
  170. fi
  171.  
  172. exit 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement