As a consultant, I frequently move my laptop between networks. Most of the time leaving everything set to ‘automatic’ e.g. using DHCP to configure all of my lan settings at boot, works fine. However at home I use OpenDNS to control my children's access to the web, and to help filter out known malicious sites etc.
(If you have children, or less than savvy users of any age at home I would encourage you to have a look at OpenDNS. The free account should be sufficient for most home users.)
Because I want most systems at the house to use the OpenDNS DNS servers, I have placed their addresses in my routers DNS entries. As my router also serves as my DHCP server on my home network this means any client using DHCP to configure itself acquires those DNS servers. There are times when I need to access a site that would normally be blocked due to the settings I have chosen for my account on OpenDNS. So I used to manually go in and change the DNS servers for my connection back to my ISP’s servers. Which meant about a half dozen clicks and some typing each time. As a programmer, repetitive tasks demand to be automated!
So I began looking for how I could automate the necessary steps in a command or batch file. I finally found a series of commands that appear to work just fine for XP, Vista and 7.
Here are the commands to reset your Local Area Connection to DHCP for both IP address and DNS servers:
netsh interface ip set address name="Local Area Connection" dhcp
netsh interface ip set dnsservers name="Local Area Connection" source=dhcp
When I get to a network where I want to set just the DNS servers I use the following commands:
netsh interface ip set dns name="Local Area Connection" static 156.154.70.22
netsh interface ip add dns name="Local Area Connection" 156.154.71.22 index=2
You would want to specify the DNS servers for your ISP here instead of mine of course. Note that the syntax changes for the secondary DNS server entry. This took some digging and trial and error to finally get working. I am sure it is documented somewhere, but I could not find a clear explanation anywhere. The main location for the documentation I did find is here on TechNet
Under Vista and 7 you need to run these batch files as an Administrator, I just right click and select the option to run as… from the context menu, answer the UAC prompt and they run.
There are equivalent settings for the subnet mask, IP Address, and Gateway:
netsh interface ip set address name="Local Area Connection" source=static addr=192.168.1.10 mask=255.255.255.0 gateway=192.168.0.1
In the above command the first address is the IP address for the connection, the second is the subnet mask, and the third is the gateway address.
I hope this helps if you move between IP configurations frequently!
Cheers,
Bob Porter