10
1
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?
Are you sure you require changes to the hosts file as opposed to e.g. modifying the host entries via
dscl
directly? – Daniel Beck – 2012-01-22T16:39:03.017I was not aware of the
dscl
command, I will have to research on that as well. – Lasse V. Karlsen – 2012-01-22T18:36:32.343See e.g. here. Internally, OS X uses its directory services, which you can control using
– Daniel Beck – 2012-01-22T18:39:06.847dscl
, 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 todscl
and flushes the cache might work better in your situation than keeping multiple copies of the hosts file, or rewriting it all the time.@DanielBeck: Good point! I wasn't aware of all the capabilities of dscl. – Karolos – 2012-01-22T19:33:45.777
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 – 2012-01-22T19:35:13.777
@LasseV.Karlsen: Where you able to do what you wanted ? If yes, how did it succeed ? – Karolos – 2012-02-13T19:08:03.030
@Karolos: I ended up writing a small script that concatenates several files into one hosts file, and then just empty out the relevant files before running it. – Lasse V. Karlsen – 2012-02-13T21:52:47.297