Hosts File Utility

Monday, April 27 2009

I hate hosts.txt. I have to modify my host entry at least 20 times a day. I have entries for production, qa, staging, and of course dev. In fact, I have around 50-100 entries in my hosts file. I try and keep it organized, but it is still a hassle. There have been many times where I thought I was pointing to one environment, but in reality I was pointing somewhere else. So, I’ve decided to create a small utility app that will help me out.

Ideally this would be a simple scripting app Monad maybe? Maybe not. I’ve decided to use a VS 2008 command line app. At some point I may consider putting an GUI front end on it, but first things first. Changing a hosts entry today isn’t all that hard:

  • Click on the host short-cut
  • Scan file for entry
  • Remove or add #
  • Save

It’s really just those four steps. Of those, only the second step is really a pain point. The other major pain point is knowing which environment I am pointed to. Today I use a number of things to determine that:

  • Specific item on a web page – requires me to go to a page I don’t need to render
  • Header on the page – requires me to inspect the headers
  • Scan the hosts file – requires me to visual inspect the hosts file

My command line app needs to be able to quickly tell me which environment is currently “active”. If it takes longer than the manual process, what’s the point?

So my initial requirements:

  • Open hosts file
  • Read hosts file
  • Save hosts file
  • Add new entry (IP address and host name)
  • Comment out entry
  • Uncomment entry
  • List currently active
  • List by hosts name including which one is active

The first three bullets are implicit actions that just need to happen. The last five need a closer look though.

This is a command-line utility so I need to define the parameters/flags that will be used to determine what the application will do.

Add New Entry

hostutil.exe -a 192.168.1.1 www.codeverity.com

Comment out entry

hostutil.exe –c 192.168.1.1

I have a fairly large number of IP addresses to keep track of. It would be nice if I could pass the host entry itself and a partial IP match that would comment out all host entries with the matching host name and partial match of IP address:

hostutil.exe –c 127. www.codeverity.com

All IPs for given host entry:

hostutil.exe –c www.codeverity.com

Uncomment entry

hostutil.exe –u 192.168.1.1

or

hostutil.exe –u 127. www.codeverity.com

All IPs for given host entry:

hostutil.exe –u www.codeverity.com This wouldn’t make sense since only one entry is going to be used.

List Currently Active

hostutil.exe –l

or

hostutil.exe –l www.codeverity.com

or

hostutil.exe –l 127. www.codeverity.com

 

Now I have enough to get started. I have a few more nice to have requirements, but this basic list will go a long way towards making my development life easier.

In part II I’ll put up some code.