Apr 17, '12 07:30:00AM • Contributed by: desepticon
The first script gets the active network interface. This is called by the second script which determines whether to run a matched script for either the Ethernet or AirPort interfaces. These third and fourth scripts restart either the Ethernet or AirPort connection respectively. These last two scripts can also be configured to send you an e-mail if this happens by altering the 'tobenotified" string to reflect your e-mail address in each of them. Be sure to maintain the single quotes. Or, if you do not wish to receive e-mail, comment these parts out.
Name the first script "active_interface.sh", and the second "check_internet_master.sh". This second script is what controls the rest. Attach it to lauchctl to do this automatically and never worry about a lost connection. The third and fourth scripts should be called "check_internet.sh" and "check_internet2.sh". Of course you can always name these whatever you want, but be sure to put the path to them in the scripts. Make sure to chmod all the files executable; and, unless the scripts are altered, they should also be run from the same folder.
In order for ifconfig to run with out intervention, the script needs to be run as root. Alternatively, you could make an exception for ifconfig in your /etc/sudoers file by adding the line (if you run as admin - else put %users):
%admin ALL= NOPASSWD: /sbin/ifconfig
I don't think this is too much of a security issue.
With this setup, I find I am nearly impervious to lost connections.
============================================================================================= #!/bin/bash # This script determines which script to run # based on whether your running on # ethernet or airport. # Name check_internet_master.sh or similar # Attach this script to launchctl to run automatically. AI=`./active_interface.sh` if [ "$AI" = "en0" ]; then echo "check_internet_master has detected" $AI "as your primary network interface. Launching matched script..." ./check_internet.sh else echo "check_internet_master has detected" $AI "as your primary network interface. Launching matched script..." ./check_internet2.sh ============================================================================================= #!/bin/bash # Script to restart the ethernet if there is no internet connection # Name this check_internet.sh # Alter tobenotified to reflect you email address. IS=`/sbin/ping -c 5 74.125.226.18 | grep -c "64 bytes"` tobenotified='xxxx@xxx.com' if (test "$IS" -gt "2") then echo "Your internet connection appears to be working. Code" $IS internet_conn="1" exit else echo "There appears to be a problem with your internet connection. Will check again in 10 seconds... Code" $IS internet_conn="0" sleep 10 AA=`/sbin/ping -c 5 74.125.226.18 | grep -c "64 bytes"` if (test "$AA" -gt "2") then internet_conn="1" echo "Your internet connection appears to be working now. Code" $AA exit else echo "There is still a problem with your internet connection. Attempting to fix by ethernet restart... Code" $AA sudo ifconfig en0 down sleep 10 sudo ifconfig en0 up echo "Your internet connection needed to be restarted" | mail -s "Internet Connection Down on $(date '+%m/%d/%y @ %H:%M:%S')" $tobenotified -f ipdown@no-reply.com -F "Ethernet Connection Problem" fi fi ============================================================================================= #!/bin/bash # Script to restart the airport if there is no internet connection # Name this check_internet2.sh # Alter tobenotified to reflect you email address. IS=`/sbin/ping -c 5 74.125.226.18 | grep -c "64 bytes"` tobenotified='xxxx@xxxx.com' if (test "$IS" -gt "2") then echo "Your internet connection appears to be working. Code" $IS internet_conn="1" exit else echo "There appears to be a problem with your internet connection. Will check again in 10 seconds... Code" $IS internet_conn="0" sleep 10 AA=`/sbin/ping -c 5 74.125.226.18 | grep -c "64 bytes"` if (test "$AA" -gt "2") then internet_conn="1" echo "Your internet connection appears to be working now. Code" $AA exit else echo "There is still a problem with your internet connection. Attempting to fix by airport restart... Code" $AA sudo ifconfig en1 down sleep 10 sudo ifconfig en1 up echo "Your internet connection needed to be restarted" | mail -s "Internet Connection Down on $(date '+%m/%d/%y @ %H:%M:%S')" $tobenotified -f ipdown@no-reply.com -F "Airport Connection Problem" fi fi =============================================================================================
