Skip to content

Manage your hosts file from the command line.

  • by

Author: smeans
Go to Source

When you’re a full-stack developer, you often find yourself mixing and matching destination hosts on your development system. Sometimes you need to run your development codebase against a test back-end server, sometimes you need to spoof the test back-end with your local code base. All of this switching back-and-forth can be tedious, so I wrote hostmod.py to make my life easier.

It is a really simple command-line tool that can automatically comment/uncomment lines in your hosts file on either Windows or Linux. To use it, you just add the records you want to be able to toggle to your hosts file using your editor of choice. Then, you can enable/disable those lines by running hostmod.py like so:

./hostmod.py -u on myhostname # uncomments the myhostname line
./hostmod.py -u off myhostname # comments out the myhostname line
./hostmod.py -u toggle myhostname # toggles myhostname state
./hostmod.py list # dumps the current hosts file contents

For safety’s sake, you must include the -u option to have the utility rewrite your hosts file, otherwise it will just dump the new hosts file to stdout. It’s not super-fancy, but it gets the job done. Again, you can find the gist for the script here, hope it helps you out!

Read more