Get windows to know whether it is at home or not

The problem:
I have a server at home that I access with internal IP 192.168.etc.etc. I have a dynamic domain thanks to dyndns in the form of So when I want to contact my server from outside my home network I use and when I want to access it from home I have to use the internal IP. That’s a bit of a pain, isn’t it? My home router is not clever enough to understand that when I contact from within my network I want it to be sent to my home server. My router just freaks out and does nothing. I have 3 computers at home. A linux, a mac and a windows. The linux and mac ones always stay home so I don’t need to bother. The windows though is my work computer and is always travelling, so I needed a script to check whether I am at home and modify the hosts file to map to the internal IP address of the server when I am at home, and to reverse it once I’m out.

Here is what I did, I’m sure there are more clever ways but my windows scripting skills are equal to zero, I have googled it quite a bit but haven’t found a solution, so I decided to learn the basics of windows batch scripting and give it a try.

working folder is: C:\batch
I have created one file called hostsupdate.bat. In it there is:

IPCONFIG |FIND /C "192.168.X.X">%TEMP%\temp.txt
if errorlevel 1 C:\batch\switch_remote.bat
if errorlevel 0 C:\batch\switch_local.bat

Where 192.168.X.X is the internal IP of my windows computer when I am at home (I have configured my wireless router to always assign the same IP to the mac address of my windows box).
I wonder if I need to add an EXIT at the end of this .bat file.

Then file switch_remote.bat

if exist C:\WINDOWS\system32\drivers\etc\remote exit
del C:\WINDOWS\system32\drivers\etc\hosts
copy C:\WINDOWS\system32\drivers\etc\hosts.remote C:\WINDOWS\system32\drivers\etc\hosts
echo 1> C:\WINDOWS\system32\drivers\etc\remote
 del C:\WINDOWS\system32\drivers\etc\local

and finally file switch_local.bat.

if exist C:\WINDOWS\system32\drivers\etc\local exit
del C:\WINDOWS\system32\drivers\etc\hosts
copy C:\WINDOWS\system32\drivers\etc\hosts.local C:\WINDOWS\system32\drivers\etc\hosts
echo 1> C:\WINDOWS\system32\drivers\etc\local
del C:\WINDOWS\system32\drivers\etc\remote

Same here, do I need EXIT? Oh well, so far it looks OK.

you need to create the two template hosts files you need for when you are remote (C:\WINDOWS\system32\drivers\etc\hosts.remote) and when you are home (C:\WINDOWS\system32\drivers\etc\hosts.local).
Writing that I see that I should have placed those templates in my batch folder so that I can easily back it up and re-use it on another computer one day. I can’t be bothered.

Once all of that is done you can execute hostsupdate.bat. It will check whether you have a home style IP, and if yes it will check if there is a ‘lock file’ saying that your hosts file is already in ‘local mode’, if that’s the case it’ll exit, if not it will switch the config to ‘remote mode’.

Now, I run that every 10 min via the tasks scheduler, to keep it in silent mode I use Hidden Start. It took me quite a few attempts before getting it right in the tasks scheduler, so here is how it is in the “Run:” section:
C:\batch\hstart.exe /NOCONSOLE “hostsupdate.bat”

hostsupdate screen capture

Leave a Reply

Your email address will not be published. Required fields are marked *