The Joys Of Hacking ‘hosts’

In my job I am constantly visiting client offices where you need to know a number of IP addresses that aren’t often kept in the local DNS, even if they have one.

Recently I had a revelation, my computer already holds a (very short) list of names to be translated into IP addresses. The ‘hosts’ file in /etc/ – here’s what it looks like on your Mac:

# Host Database
#
# localhost is used to configure the loopback interface
# when the system is booting.  Do not change this entry.
##
127.0.0.1   localhost
255.255.255.255 broadcasthost
::1             localhost

Short, right? Not terribly useful, but we can easily fix that.

I do this in a way that makes it easy to share the file with other engineers in the company. First you will need a directory etc in your home directory. I use this for all the bits and pieces I have that result in changes to the system /etc directory.

Copy /etc/hosts to ~/etc/hosts.head.

Now I write a hosts file for my home network as we may as well have the same benefits we get at work while we are hacking at home. Create a file called hosts.home in the same directory with names and IP addresses. Here is part of mine:

# Home
192.168.1.1     hm-router   # core router
192.168.1.15    hm-air      # The Air
192.168.1.41    hm-fetch    # The Fetch-TV
192.168.1.111   hm-imola    # Imola - the iMac
192.168.1.136   hm-express  # The Airport Express
192.168.1.183   hm-hue      # Philps Hue lights
192.168.1.198   hm-xap      # Xirrus WAP

Finally we get to the same list but this time for our customers. Call this one hosts.work. Here is mine:

###
### Work hosts file
###
### It is perfectly acceptable to have duplicate IP addresses in
### the list, the Mac won't bork, but duplicate names is bad juju
### therefore start with short prefix per customer/site.
###
### Of course if you aren't connected to the client net either physically
### or via VPN then these aren't exactly useful.
###
### Tony Williams (ARW)
###
### 0.1 19/10/2015 ARW First Concept
### 1.0 19/10/2015 ARW Tested, extended with Tiny Custmer and added notes.
###
### Please don't forget the comment at the end.
### Keep one customers list in the one spot and
### please add new customers in alphabetical order.
### If you edit then please rev the version number,
### and date, put your initials and quick note.

# Medium Customer (mc)
192.168.101.22  mc-mini         # existing Mini server
192.168.101.17  mc-hp-1950      # HP 1950 switch
192.168.101.19  mc-hp-box       # HP Server ILO
192.168.101.41  mc-esxi         # ESXi running on hp-box
192.168.101.26  mc-ws2012       # Windows Server 2012 running in ESXi
192.168.101.254 mc-router       # Core router

# Tiny Customer (tc)
10.161.150.101  tc-xserve       # The original XServe
10.161.151.23   tc-nas          # The new NAS

# Company (com)
192.268.2.3     com-hv      # the Hyper-V Host
192.168.2.5     com-server  # The File Server
192.168.2.6     com-dc      # Domain Controller
192.168.2.20    com-nas     # The Seagate NAS-4

Now to install our file all we need do is sudo cat ~/etc/hosts.* > /etc/hosts and the new hosts file is built and in place. Given our file names you will find that the original is now at the top followed by your home network and finally the work entries. If you like you can even add an alis to that in your bash profile – alias build-hosts=sudo cat ~/etc/hosts.* > /etc/hosts

You will be amazed at the places you can now use these names. Your web browser, the “Go To Server…” dailog in the Finder, they even work with such tools as ping, oh and if you have bash completion installed then completion works with ping and these names. One of the real benefits on a Mac is that you can store the credentials you use in “Go To Server…” and never have confusion between customer sites and “KeyChain Access” becomes a tiny database of passwords as well. (This is why we File Vault2 a work laptop people.)

Now to make them even more usueful for you. Here’s a little bash function to add to your bash profile:

hosts()
{
grep -i "$*" /etc/hosts
}

The -i tells grep to ignore case (don’t need to rach for that shift key when typing) and the "$* takes the rest of the line you type and wraps it in quotes so that you can search for two or three word strings.

Add that in and then (if we are using my example file) hosts com at the command line will get you a list of hosts for “Company”. Can’t remember the prefix for “Tiny Company”, then hosts Tiny Company or even hosts tony company will show you the top line of the table for that client with the little reminder at the end .

Another benefit of having all that information in one place rather than scattered through client notes and the way we build it is that you and your fellow engineers can share the same file among you, each adding their regular clients and making it much easier when you have to visit one of theirs.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s