The Constant Innovator


Changing IP address and DNS settings programmatically in Windows Vista
May 26, 2009, 2:22 pm
Filed under: Uncategorized

(long post title, but it describes it perfectly)

Background: I’ve run into the situation where I have multiple IP/DNS “profiles” that I want to switch between on my laptop, and find it an enormous pain to go into Network Settings again and again. One of my colleagues had a bigger problem when his office IP settings did not work at all between his home and office.

Solution: This is a Batch file (.bat) meant for Windows Vista that allows you to select from different profiles and set IP and DNS settings accordingly. You will have to click on the User Account Control prompt and select Continue.

Setup

  1. Copy the code below into a file: “IP-DNS-switch.bat”
  2. Change the contents to fit your requirements. Do NOT blindly copy paste this and expect it to work. It will NOT!
  3. Move the bat file into C:\Windows (not required to be there in particular, but it would be the safest location from accidental deletion)
  4. Create a shortcut to this file on your Desktop
  5. Edit the Properties of this shortcut and in Advanced, set “Run as administrator”

Notes

  1. This is meant for Windows Vista. It won’t work anywhere else.
  2. The settings are for your default wireless connection, you’ll need to modify the file appropriately if its anything else.
  3. Again, the IP and DNS settings are meant for MY connection ONLY, you MUST modify these. Don’t even THINK of using them as it is.
  4. To understand more about netsh, open up a Command Prompt in Windows Vista and type the following:
netsh int ipv4 set address /?
netsh int ipv4 set dnsserver /?
netsh int ipv4 add dnsserver /?

References

  1. http://en.kioskea.net/forum/affich-13513-how-to-change-ip-address-from-command-prompt
  2. Various sites on how to use the CHOICE and NETSH command
  3. Windows Vista command prompt with the /? operator

The main code

@echo off
REM IP-DNS-switch.bat
REM Copyright(c) 2009 Siddhant Bhansali
REM VBScript to automatically switch between Home and Work IP and DNS configurations.
REM Pre-configuration:
REM 1. Change the IP and DNS settings to whatever is appropriate to your networks
REM 2. Copy this file to C:\windows so that it is not accidentally deleted
REM 3. Create a shortcut to it on the Desktop
REM 4. Right click and select properties of the shortcut
REM 5. In Advanced, set "Run as administrator"
REM 6. Save these settings and you're done!
REM --------------------------------------
echo.
echo Switch to which configuration?
echo.
echo [H]-Home (All IP and DNS settings set to Automatic)
echo [W]-Work (IP and DNS settings set to Work settings)
echo [E]-Exit (No change)
echo.
CHOICE /C:HWE

IF ERRORLEVEL 3 GOTO END

IF ERRORLEVEL 2 GOTO WORK

netsh int ipv4 set address "Wireless Network Connection" source=dhcp
netsh int ipv4 set dnsserver "Wireless Network Connection" source=dhcp
pause
goto END
:WORK
netsh int ipv4 set address "Wireless Network Connection" static 192.168.1.XX 255.255.255.0 192.168.1.1 1
netsh int ipv4 set dnsserver "Wireless Network Connection" static 203.94.243.70 primary
netsh int ipv4 add dnsserver "Wireless Network Connection" 203.94.227.70
pause
:END
Advertisements

Leave a Comment so far
Leave a comment



Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s



%d bloggers like this: