command line/batch changing from static IP to DHCP

Soldato
Joined
19 Jul 2005
Posts
7,069
Location
S. Yorkshire
Hi, I work between two location. In the first location I must use a static IP, in the second I must be on DHCP.

At present I am manually changing to static when needed, but it's a faff.

Is there an easy way to generate a batch file I can run that enters a predefined set of address information (IP, SN, GW) and another I can run to set it back to DHCP? I do this several times a week so it's a headache!
 
I used to use a program called IP Switcher and you can save multiple profiles. The app just used to sit in the systray and you would right click it and change to whichever profile you wanted.
 
Can't you use the Alternate IP config to put in a static address and leave the main as DHCP, this way if there's no DHCP server found and it times out it will use the Alternate IP config.
 
I'm on Windows 7 which isn't supported on ipswitcher and both sites have DHCP so alternate config won't work.
 
Ideally you need to ask whoever controls DHCP on the site where you need a static address to give you a DHCP reservation.

If not, try these:

@Echo off
netsh interface ip set address name="Local Area Connection" static 192.168.0.100 255.255.255.0 192.168.0.1 1
netsh interface ip set dns "Local Area Connection" 192.168.1.1
netsh interface ip add dns "Local Area Connection" 192.168.1.2 index=2
pause


This assumes your interface is called "Local Area Connection", and the IP address is 192.168.0.100, mask 255.255.255.0, gateway 192.168.0.1.
Second and third lines would set your DNS servers to 192.168.1.1 and 192.168.1.2 respectively - adjust to suit.


To set the interface back to DHCP use:

@Echo off
netsh interface ip set address name="Local Area Connection" dhcp
pause
 
Last edited:
So thats basically exactly the same as the link i posted, 2 batch files to netsh changes to the nic.
 
Thanks, they are both good suggestions. I'm going to use PistolPete's as I can just copy and paste into batch files. Thanks for both suggestions though!
 
No, just very lazy! It works perfectly. Well, the DNS line failed, but I've worked around that!
Thanks indeedy!
 
You cant add a second IP address if its setup as DHCP.

You can on XP, this is exactly how I have my laptop setup, DHCP for when I'm in the office and my Alternate Configuration tab has a static IP for when I'm at home. Effectively you're setting up a manual APIPA address so instead of getting a 169 IP address I get a 192.

More info here http://support.microsoft.com/kb/283676
 
Last edited:
Back
Top Bottom