Avatar of hypercube
hypercube
Flag for United States of America asked on

WIN 7 .bat file to change IP address .. DHCP problem

I have a few batch files that I use to switch NIC settings.  These are all "Run as Administrator".

For static IP, I use this:
@ECHO OFF
set varip=192.168.0.99
set varsm=255.255.255.0
set vargw=192.168.0.1
set vargwmetric=1
set vardns1=192.168.0.1
set vardns2=8.8.8.8
set vardnsindex1=1
set vardnsindex2=2

REM ***** You don’t need to change anything below this line! ******
ECHO Setting IP, mask, gateway and DNS
REM netsh set address "Local Area Connection" static %varip% %varsm% %vargw% %vargwmetric%
netsh interface ip set address "Local Area Connection" static %varip% %varsm% %vargw% %vargwmetric%
ECHO Setting Primary DNS
netsh interface ip set dns name="Local Area Connection" source=static address=none
ECHO Setting primary DNS
netsh interface ip add dns  name="Local Area Connection" %vardns1% index=%vardnsindex1%
ECHO Setting secondary DNS
netsh interface ip add dnsserver "Local Area Connection" %vardns2% index=%vardnsindex2%
ECHO Here are the new settings for %computername%:
netsh int ip show config
pause
end

Open in new window


To switch to DHCP "get an address automatically", I use this:
@ECHO OFF
REM ***** You don’t need to change anything below this line! ******
netsh interface ip set address name="Local Area Connection" dhcp
netsh interface ip set dns name="Local Area Connection" dhcp
netsh interface ip set default gateway name="Local Area Connection" dhcp
netsh interface ip show config[embed=file 906425]
pause
end

Open in new window

The problem is, that when I switch from a static address to a DHCP configuration, the default gateway IP address remains and the DHCP button isn't turned on - and the NIC settings dialog appears to disagree with the results of ipconfig that says DHCP YES!

ipconfig
At the same time, the NIC ipv4 settings look like this:
NIC settings
What might be the problem here?
DHCPProgrammingNetworking

Avatar of undefined
Last Comment
hypercube

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
Zephyr ICT

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
SOLUTION
Gerwin Jansen

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
hypercube

ASKER
I tried those things but nothing in the results has changed.

ipconfig result and the GUI result are different re: DHCP.
Gerwin Jansen

This is Windows 7 right? I tested on W7 64bit machine and it works just fine? Any error message you see in Windows event log maybe?
hypercube

ASKER
reboot...  :-(
Everything works now.
Thanks all!
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
hypercube

ASKER
Thanks again!