Skip to content

Instantly share code, notes, and snippets.

@colincwilliams
Last active July 5, 2016 06:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save colincwilliams/9d19e7bbb0c6c00e9c3f220e530db7d0 to your computer and use it in GitHub Desktop.
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.
-- 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
@colincwilliams
Copy link
Author

Explanation can be found on my website.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment