Do you like this site? Remember to share it to all your friends on Facebook and Twitter!

Saturday, July 2, 2011

Command lines to setup IP address and DNS of Windows PC

When I use my Windows notebook as connectivity tester, I often have to switch the IP address configuration from DHCP to some fixed IP, and back and forth. Also, different customers have different assigned fixed IP addresses.



It is very annoying using GUI to change the configuration repeatedly. It also creates lots of typos to add up more troubleshooting time. I decided to create some automation. If I can create small scripts or batch files to do such routines for me, it will save me so much time!

After some research and try-and-error, I found NETSH, and I also learned something:

  1. When we use NETSH to switch back to DHCP, we must issue two commands: one for IP address, and one for DNS.
  2. NETSH command "set dnsserver" replaces all previous DNS settings. Only the First DNS server can be configured using this command.
  3. To add more DNS servers, we have to use "add dnsserver ... index=2" for the Second one, and so on.

Here are the sample batch contents for you to cut-and-paste. To execute them without error, you should raise your privilege level to system administrator, especially in Windows Vista and Windows 7.

Setup PC's NIC as DHCP
netsh interface ip set address "Local Area Network" dhcp
netsh interface ip set dnsserver "Local Area Network" dhcp

Setup PC's NIC as Fixed configuration
set ADDRESS="1.2.3.4"
set    MASK="255.255.255.0"
set GATEWAY="1.2.3.1"
set  METRIC="1"

set    DNS1="192.168.0.253"
set    DNS2="192.168.0.254"
set    DNS3="192.168.0.252"

netsh interface ip set address "Local Area Network" ^
   static %ADDRESS% %MASK% %GATEWAY% %METRIC%

netsh interface ip set dnsserver "Local Area Network" static %DNS1%
netsh interface ip add dnsserver "Local Area Network" %DNS2% index=2
netsh interface ip add dnsserver "Local Area Network" %DNS3% index=3

References
Do you like this post? You really should consider Subscribing by Email!


Related Posts with Thumbnails

No comments:

Post a Comment

Tip: you can also anonymously comment here.

Popular Posts