My router is dying. How do I know? It keeps dropping my wireless connection. I’ll be away from my home server and, when my router goes down for a few minutes, it loses its wireless connection. Then a bug in OS X results in my server failing to reconnect to the network, leaving it offline until I can get to it.
This has been happening a few times a week for a couple of months. I was kind of hoping it would start happening so frequently it would give me an excuse to get a new router, but alas, the old router keeps hanging on.
To work around this issue, I wrote a script for my server that runs every 10 minutes, checking if there is an internet connection. If not, it logs it, then restarts my server’s wifi connection so it will re-connect. This has let my router live another day.
Logging the disconnects has allowed me to get a handle of just how frequently my network is dropping (on average once per day so far), and therefore properly evaluate when to get a new router.
Below I walk through the script; next time I’ll go over how I automated its execution.
Since my home server is a Mac, I wrote the script in AppleScript. View and download the full script here.
Lines 2-29 redundantly attempt to ping the OpenDNS servers, noting if there’s a connection. I grabbed this code from an answer on Stack Exchange.
set connected to false -- Ping the primary OpenDNS server. try set pingResult1 to do shell script "ping -c 1 126.96.36.199" 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 188.8.131.520" 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
Line 31 simply sets the path to the log file. I’m keeping it on my desktop and have named it reconnectLog.txt.
set logFile to (path to desktop as text) & "reconnectLog.txt"
If there isn’t a network connection, line 34 logs the date and time the disconnected state was encountered, using the appendToDisk function defined at the bottom of the script.
if not connected then appendToDisk from ((current date) as text) & " Disconnected " into logFile
Lines 38-40 attempt to reconnect by restarting AirPort, OS X’s WiFi service. To do this, commands must be run in the shell using the do shell script syntax. I inserted a delay of 3 seconds between turning AirPort off and turning it back on to improve reliability. Sometimes it wouldn’t turn back on if the call was made too quickly. I grabbed this code from a StackOverflow question.
do shell script "networksetup -setairportpower AirPort off" delay 3 do shell script "networksetup -setairportpower AirPort on"
If there was a connection, line 42 logs the date and time the check was performed.
else appendToDisk from ((current date) as text) & " Connected " into logFile end if
Lastly, the function appendToDisk is defined on lines 47-57. This takes theText and appends it to the file located at thePath, while handling errors. I grabbed this code from an answer on Stack Overflow.
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