Geeks With Blogs
BlogOfBob Read:Learn:Contribute:Repeat

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

Posted on Thursday, December 10, 2009 8:10 PM Windows 7 , Ramblings | Back to top


Comments on this post: Changing your DNS and IP information via script

# re: Changing your DNS and IP information via script
Requesting Gravatar...
I had the same problem. During my travels I have to move between multiple locations. And each provider has their own DNS settings. So before reading your post I had to go through bunch of control panel windows to get to the DNS settings. With you script it is so much easier. I can just have one script for each location and have it all set! Thanks!
Left by 192.168.0.1 on Apr 27, 2010 3:39 AM

# re: Changing your DNS and IP information via script
Requesting Gravatar...
great article! thanks so much -- your advice is going to make the rollout to 1500 endpoints so much easier :)
Left by Chris on Jul 09, 2013 3:10 PM

# re: Changing your DNS and IP information via script
Requesting Gravatar...
Thanks a bunch, was looking for this for months....
for windows 8 replace name="Local Area Connection" with name="Ethernet"
Left by Inno on Sep 05, 2013 11:09 AM

Your comment:
 (will show your gravatar)


Copyright © Bob Porter | Powered by: GeeksWithBlogs.net