Change Hosts file based on connection - Windows

08
2014-07
  • WernerCD

    So I have a laptop. It connects to places wirelessly. Home, work and public. I want to change the host file based on the network I'm connected too.

    Realistically, in my situation, I should have 2 sets of IPs:

    Home Network (Inside Network):

    192.168.1.101 HomePC
    192.168.1.102 HomePrinter
    

    Home Network (Outside Network):

    1.2.3.4 HomePC
    1.2.3.5 HomePrinter
    

    Work (Inside Network):

    192.168.50.101 WorkPC
    192.168.50.102 WorkPrinter
    

    Work (Outside Network):

    70.2.3.4 WorkPC
    70.2.3.5 WorkPrinter
    

    I need an easy way to toggle between the 3 states, preferably automatically based on Hotspot I'm connected too.

    Powershell scripts, open-source, etc preferred, but I'm not adverse to spending if the best tool is affordable

  • Answers
  • SiamKreative

    So far here is what I found:

    • Mobile Net Switcher, which does much more than just switching hosts file. It costs 29€.
    • Switch Location (developed by Kevin Blake), which is totally free, but seems to be crashing on Windows 8.1 x64.

    Did you find anything else?


  • Related Question

    networking - Is the hosts file ignored in windows if DNS Client service is running?
  • Mnebuerquo

    I've seen a number of articles about how to edit the hosts file in Windows 7, but it's all about how to open notepad as administrator, not the actual behavior of the dns lookups afterward.

    I've read that the hosts file is ignored in XP SP2 if DNS Client service is running. I have tried this on my XP machine and it seems to be true. I can see how it is a security danger to have a hosts file that user programs could modify. If it could write to hosts, then any malware could spoof dns locally with minimal difficulty.

    I'm trying to use the hosts file for testing stuff on my local network without it going to the live site on the internet. At the same time I want to be able to use dns on the normal internet.

    Mostly though I just want to understand the rules on the newer windows systems.

    Thanks!


  • Related Answers
  • Fred

    XP uses the hosts file for name resolution even when DNS client is running. The contents of the hosts file are automatically loaded into the DNS resolver cache when you save the file.

    The Name Resolution Policy Table article says the same thing for Win 7:

    Here is how the name resolution process works for Windows 7 and Windows Server 2008 R2:

    1. An application uses the DnsQuery() API or the GetAddrInfo() or GetHostByName() Windows Sockets APIs to resolve a name. If the name is a flat name, the DNS Client service creates an FQDN using configured DNS suffixes.
    2. The DNS Client service checks the DNS resolver cache for the FQDN, which contains the entries in the Hosts file and the results of recent positive and negative name queries. If an entry is found, the result is used and no further processing occurs.