Mac OS X "hosts" file, can I include other files with it?
Rise to the top 3% as a developer or hire one of them at Toptal: https://topt.al/25cXVn
--------------------------------------------------
Music by Eric Matyas
https://www.soundimage.org
Track title: Industries in Orbit Looping
--
Chapters
00:00 Mac Os X &Quot;Hosts&Quot; File, Can I Include Other Files With It?
01:31 Accepted Answer Score 10
02:10 Answer 2 Score 0
02:42 Answer 3 Score 0
03:35 Thank you
--
Full question
https://superuser.com/questions/381138/m...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#macos #osxlion #automation #hosts
#avk47
ACCEPTED ANSWER
Score 10
I am not aware of any include possibility. What I'd do, however, is to make sections in my hosts file, and then use a script to comment the lines in each section using, e.g., sed.
This way your file would look like
#%%%HOME.HOSTS%%%
#Put here the contents of home.hosts
#%%%WORK.HOSTS%%%
#Put here the contents of work.hosts
#%%%ALWAYS_ON%%%
127.0.0.1 localhost
::1 localhost
Edit: Adding in a quick attempt to modify the fields.
Removing the comment for HOME.HOSTS
sed -i '/#%%%HOME.HOSTS/,/#%%%/s/^#\([^%]\)/\1/g' hosts
Putting back the comments for HOME.HOSTS
sed -i '/#%%%HOME.HOSTS/,/#%%%/s/^\([^#]\)/#\1/g' hosts
This is a basic version, and needs to be adjusted to your needs.
ANSWER 2
Score 0
Since dscl is no longer working due to the switch to OpenDirectoryService, one option is to use ghost:
sudo gem install ghost
sudo ghost add fw.test.se 10.0.0.1
...will add this section in the /etc/hosts:
# ghost start
10.0.0.1 fw.start.se
# ghost end
To clear the #ghost section of the hosts file use (this makes it easy to replace the list of hosts with a new one):
sudo ghost empty
You can find more information about ghost here: https://github.com/bjeanes/ghost
ANSWER 3
Score 0
I had a similar use case when I needed to access services on three different places using different LAN / WAN settings in hosts file. Karolos' answer above is one possible solution. My approach is here.
1- ref to http://apple.stackexchange.com/q/139267/74657
2- created hosts files for each location (hosts.work, hosts.home, hosts.vendor)
3- Using soloz's script, here (https://github.com/slozo/Network-listener) whenever I connect to home network, I execute simple script to switch the hosts file.
simplest method would be
mv hosts.home /etc/hosts
alternately you can automate Karolos' answer above using sed to update the exiting file on connecting to specific SSID which you can detect and run easily by using soloz's script mentioned above.
hope that helps anyone stumbling upon the subject issue.