HolisTech Limited. For fun and clever people

hostsedit.exe a windows command line hosts file editor

This software is released under the GNU GPL (General Public Licence)

Summary

hostsedit.exe is a simple way to add and remove entries to the windows hosts file. If you aren't sure what that means then you probably don't need it. It is used to automate changes to unusual network configurations.

Downloads

Source code, a precompiled exe and a zip are available

Details

It looks for the hosts file under [windows system directory]\devices\etc\hosts then [windows directory]\hosts. If it finds the file then it adds or removes entries as specified by the command line arguments

Basic Options

  • hostsedit append mymachine 127.0.0.2 will add a host file entry associating the machine name "mymachine" with the address 127.0.0.2
  • hostsedit remove mymachine 127.0.0.2 will remove the hosts file entry associating the name mymachine with the address 127.0.0.2
  • hostsedit remove mymachine will remove any line in the hosts file containing the string "mymachine". Use with care
  • hostsedit update mymachine 127.0.0.2 will first remove any lines in the file containing the string "mymachine" then append a new line associating mymachine with the address 127.0.0.2

OpenVPN options

The openvpn-up and openvpn-down options are designed to work as up and down scripts for OpenVPN. A configuration file could look like:

up "hostsedit openvpn-up secureserver"
down "hostsedit openvpn-down secureserver"

This will add and remove a hosts file entry named secureserver associated with the remote IP address of the OpenVPN tunnel. The IP address is read from the environment variables, as under windows the exact position of the parameters passed to the up script can vary depending on the associated adapters name (spaces in the name cause problems)

Alternatives

awk or sed could both edit the hosts file, but neither have a good way to find the file name