Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

This comment has been minimized.

Copy link
Owner Author

colincwilliams commented Jul 5, 2016

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
You can’t perform that action at this time.