11

I am working on some automation scripts to configure my network settings, hosts file contents, and /etc/resolver files, when accessing different networks I frequent between.

There are some combinations that can occur that all require changes to the hosts file, and I'd like to avoid having to set up a matrix of different combinations, duplicating the shared settings all over the place.

For instance, I may be on the work network, ie. in the building, or I may access it over VPN. In both cases, I need to add some settings to the hosts file for networking to work properly, some are shared, some are not. Additionally, if I'm over VPN, I may be at home, in which case there are some other settings in the hosts file I also want to add.

As such, I was hoping that instead of creating one file for "home, accessing work over VPN" vs. "home, not accessing work", etc., is there a way for me to include other files?

For instance, let's say the following hypothetical syntax works:

#!include home.hosts
#!include work.hosts

127.0.0.1 localhost
::1 localhost

This way, I could simply clear out the contents of one, or both, of those two extra files, and leave the rest be.

Or, barring that, is there a better way to do this than to just build a small script that concatenates files such as those into a new hosts file, and as part of my automation setup, I first clear out some of those extra files, and then I invoke the script to rebuild the single hosts file from those extra files?

7
  • Are you sure you require changes to the hosts file as opposed to e.g. modifying the host entries via dscl directly?
    – Daniel Beck
    Jan 22, 2012 at 16:39
  • I was not aware of the dscl command, I will have to research on that as well. Jan 22, 2012 at 18:36
  • See e.g. here. Internally, OS X uses its directory services, which you can control using dscl, for which /etc/hosts is simply one of the available data sources, providing (of course) hostname/IP address mappings. Setting up a script that writes to dscl and flushes the cache might work better in your situation than keeping multiple copies of the hosts file, or rewriting it all the time.
    – Daniel Beck
    Jan 22, 2012 at 18:39
  • @DanielBeck: Good point! I wasn't aware of all the capabilities of dscl.
    – Karolos
    Jan 22, 2012 at 19:33
  • Let me know how it works, if you want to try doing it that way. Haven't tried it myself, so I don't post it as an answer. Good luck!
    – Daniel Beck
    Jan 22, 2012 at 19:35

3 Answers 3

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.

2
  • Hello Karolos, could you describe more about the command: sed -i '/#%%%HOME.HOSTS/,/#%%%/s/^#\([^%]\)/\1/g' hosts and about the section in hosts file please. I'm really not understand why :( Jan 16, 2015 at 2:14
  • @NNToan: Whan the line does is tell sed to add or remove a # at the beginning of the lines between two #%%% lines. This will comment or un-comment the lines. The example shows how to comment the HOME.HOSTS section using #%%%HOME.HOSTS ; you should use #%%%WORK.HOSTS instead in the sed command to comment or un-comment the WORK.HOSTS section.
    – Karolos
    Jan 18, 2015 at 16:24
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

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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .