Last active
July 5, 2016 06:12
-
-
Save colincwilliams/9d19e7bbb0c6c00e9c3f220e530db7d0 to your computer and use it in GitHub Desktop.
A short script to check for a network connection and attempt to reconnect if not.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- Lines 1-29 from: http://apple.stackexchange.com/a/35399 | |
set connected to false | |
-- Ping the primary OpenDNS server. | |
try | |
set pingResult1 to do shell script "ping -c 1 208.67.222.222" | |
on error | |
set pingResult1 to "" | |
end try | |
-- Check the results returned and return true or false. | |
set p to number of paragraphs in pingResult1 | |
if p < 5 then | |
-- Ping another Open DNS server for redundancy. | |
try | |
set pingResult2 to do shell script "ping -c 1 208.67.220.2220" | |
on error | |
set pingResult2 to "" | |
end try | |
set p to number of paragraphs in pingResult2 | |
if p < 5 then | |
set connected to false | |
else | |
set connected to true | |
end if | |
else | |
set connected to true | |
end if | |
set logFile to (path to desktop as text) & "reconnectLog.txt" | |
if not connected then | |
appendToDisk from ((current date) as text) & " Disconnected | |
" into logFile | |
-- Lines 38-40 from: http://apple.stackexchange.com/q/32926 | |
do shell script "networksetup -setairportpower AirPort off" | |
delay 3 | |
do shell script "networksetup -setairportpower AirPort on" | |
else | |
appendToDisk from ((current date) as text) & " Connected | |
" into logFile | |
end if | |
-- Lines 47-57 from: http://stackoverflow.com/a/34272894 | |
on appendToDisk from theText into thePath | |
try | |
set fileDescriptor to open for access file thePath with write permission | |
write theText to fileDescriptor starting at eof | |
close access fileDescriptor | |
on error | |
try | |
close access file thePath | |
end try | |
end try | |
end appendToDisk |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Explanation can be found on my website.