Switching IP Addresses using Batch files
Written on June 10, 2010.

Batch File

Having been provided with a client laptop from office, I need to work from home also as and when required. This created a frustrating problem for me.

When at office I had to use the assigned specific IP complete with subnet mask, default gateway and dns servers.
At home my router was configured for DHCP mode.

This let me to constantly change between static ip and DHCP when at office and when I returned to home from office.

To spare me the troubles i wrote 2 small batch files.

One was for when I was at office, which automatically changed the network adapter’s settings specific to the office and the other was for home to quickly revert back to DHCP.


I am posting both the scripts here so that if you are in the same boat as me, then you would not have to suffer like me …

Script for connection with static ip, subment mask, default gateway and dns servers provided

@ECHO OFF
set varip=xxx.xxx.xxx.xxx
set varsm=xxx.xxx.xxx.xxx
set vargw=xxx.xxx.xxx.xxx
set vardns1=xxx.xxx.xxx.xxx
set vardns2=xxx.xxx.xxx.xxx
 
ECHO Setting IP Address and Subnet Mask
netsh int ip set address name = "Local Area Connection" source = static addr = %varip% mask = %varsm%
 
ECHO Setting Gateway
netsh int ip set address name = "Local Area Connection" gateway = %vargw% gwmetric = 1
 
ECHO Setting Primary DNS
netsh int ip set dns name = "Local Area Connection" source = static addr = %vardns1%
 
ECHO Setting Secondary DNS
netsh int ip add dns name = "Local Area Connection" addr = %vardns2%
 
pause

Script output after being executed

Script output after being executed

Script for DHCP connection

@ECHO OFF
 
ECHO Resetting IP Address and Subnet Mask For DHCP
netsh int ip set address name = "Local Area Connection" source = dhcp
 
ECHO Resetting DNS For DHCP
netsh int ip set dns name = "Local Area Connection" source = dhcp
 
pause

Script output after being executed

Script output after being executed



Instructions to use the scripts :

  1. Save the scripts with an extension “.bat”.
  2. Change the xxx.xxx.xxx.xxx vales for
    • varip with the static ip provided
    • varsm with the subnet mask
    • vargw with the default gateway
    • vardns1 with the dns server address
    • vardns2 with the alternate dns server address.
  3. Execute the relevant file at the relevant place to have a hassle free change of network adapter address .

Use the script and have a nice time :)

Leave a Reply

Comment Posting Rules :
  • Please keep comments related to topic. And be nice, don't spam!

  • ✔ XHTML tags that you can use : <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">

  • Note: un-related or spam comments will be deleted.


Comments are held for moderation. If your comment does not appear altogether, do not repost. 4geeksfromnet reserves the right to remove any inappropriate or off-topic comments. If you plan on sharing helpful code, please pass it through Postable first.

Want others to know who you are? Register a Gravatar.

© Copyright 4 Geeks From Net (2006-2010).Theme modified by 7sins. All rights reserved.
For best results, please view with Firefox