» Hosts file issue

When I was first setting up wordpress on my Mac mini (which as you’ll recall is on my dedicated DSL line) I ran into a problem using the dynamic DNS name I had set-up.  With wordpress when I wanted to visit the site from inside my LAN it would automatically re-direct to the location of the set determined in settings.  This caused a problem because that location pointed to my dynamic DNS which in turn pointed to my WAN IP address and well as you can imagine wordpress go boom.

My temporary workaround during installation and initial set-up was to ignore this entirely as I was only working on the site from home.  This was short-lived as I realized I had a life and wanted to work on the site when not sitting in my home office.  Oh, yeah, and I guess I wanted the rest of the internet to have access as well – I suppose that was a factor as well.

What I ended up doing was using two hosts files, one that had the following line:

10.0.1.195     aroncares.dnsdojo.com

and another that had that same line commented out:

#10.0.1.195     aroncares.dnsdojo.com

To quickly switch back and forth I used two shell scripts, set to open by default using Terminal, that copied a “home.hosts” or “away.hosts” file on top of the hosts file.  (FYI, in Leopard, this file is located at /etc/hosts)  This worked great and was easy enough, but it didn’t have the panache that I was looking for in this situation.

The final solution that I worked out is really everything that I want (unless you include my desire to have this site hosted elsewhere) given the situation.  What I ended up doing was taking a crash course on AppleScript and writing an application that would simply ask me whether I’m home or away and act accordingly.  Interestingly I was able to give the shell script root privileges which is required to make changes to the hosts file.  For anyone that is interested I have pasted the syntax of the script after the jump.

set pwd to false

set prompt to “Where are you?”

set dialogResult to display dialog prompt ¬

buttons {“Home”, “Away”} default button 2

 

if button returned of the result is “Home” then

if pwd is false then

display dialog “Password:” with title “Password” default answer “” with hidden answer

set pwd to text returned of result

end if

set cmd to “sudo cp /etc/hosts.home /etc/hosts”

do shell script cmd password pwd with administrator privileges

else

if pwd is false then

display dialog “Password:” with title “Password” default answer “” with hidden answer

set pwd to text returned of result

end if

set cmd to “sudo cp /etc/hosts.away /etc/hosts”

do shell script cmd password pwd with administrator privileges

end if

stop

Leave a Reply

Your email address will not be published. Required fields are marked *