grab DHCP IP and set as static

April 1, 2009 at 06:01:12
Specs: XP, various
Good Morning,

We are moving from a DHCP environment to a static environment and the prospect of touching each machine (600+) does not sound nice. Is there a way to capture the current IP address assigned via DHCP and netsh it into a static IP?

thanks for looking at this post


See More: grab DHCP IP and set as static

Report •


#1
April 2, 2009 at 01:48:24
I hope this helps point you in the right direction. I am not on a works network as you are this is for my home computer. I run this once a week to give me a week on the Internet. I have ipconfig /all twice so I can see whether I have been assigned a week, if not I run it again. I also have to overcome the slight problem of the first time the command:-
netsh interface ip set address local static %IP% %Subnet% %Default% 1 is run the Default gateway does not always take so I run it twice to make sure. All that said it works and has done for many years, it can be improved maybe someone will help.

@echo off
sc config Dhcp start= auto
net start Dhcp /y
netsh interface ip set address "local area connection" dhcp
@ipconfig /release *CON*
@ipconfig /flushdns
@ipconfig /renew
@ipconfig /registerdns
@ipconfig /all >%systemroot%\ipconfig.txt
@ipconfig /all
net stop Dhcp /y
sc config Dhcp start= disabled
for %%a in (%systemroot%\ipconfig.txt) do find "IP Address" <%%a>> %systemroot%\ipconfig1.txt
for /f "tokens=2 delims=:" %%b in (%systemroot%\ipconfig1.txt) do set IP=%%b
for %%c in (%systemroot%\ipconfig.txt) do find "Subnet" <%%c>> %systemroot%\ipconfig1.txt
for /f "tokens=2 delims=:" %%d in (%systemroot%\ipconfig1.txt) do set Subnet=%%d
for %%e in (%systemroot%\ipconfig.txt) do find "Default" <%%e>> %systemroot%\ipconfig1.txt
for /f "tokens=2 delims=:" %%f in (%systemroot%\ipconfig1.txt) do set Default=%%f
netsh interface ip set address local static %IP% %Subnet% %Default% 1
netsh interface ip set address local static %IP% %Subnet% %Default% 1
CD.>%systemroot%\ipconfig1.txt
pause
shutdown -r -f -t 00 & exit


Report •

#2
April 2, 2009 at 07:08:57
That worked like a charm. I had to do very little to it (add a DNS value, basically) to make it work here. I'm going to test it in a startup script and then I think my problems are over!

thank you sooo much, you saved me a ton of trekking!


Report •

#3
April 2, 2009 at 10:55:44
You are most welcome. Glad to have helped. Obviously my dns servers do not change.
Good luck to you.

Report •

Related Solutions

#4
April 2, 2009 at 12:02:22
That might be an expedient approach, but it's a bad approach.

You should do static dhcp so that you can control the addressing at the server rather than at the workstation.

http://www.google.com/search?q=what...


Report •

#5
June 3, 2009 at 12:08:10
Hi,

For the DNS portion, how do you modify the script to grab the DNS from the ipconfig.txt file and set it static in the DNS portion?


Report •

#6
June 4, 2009 at 03:06:20
As you can see from my original postings I can only give an idea from home point of view. I have had a quick look and this works. You may have to adapt of course
@echo off & setLocal EnableDelayedExpansion
sc config Dhcp start= auto
net start Dhcp /y
netsh interface ip set address "local area connection" dhcp
@ipconfig /release *CON*
@ipconfig /flushdns
@ipconfig /renew
@ipconfig /registerdns
@ipconfig /all >%systemroot%\ipconfig.txt
@ipconfig /all
net stop Dhcp /y
sc config Dhcp start= disabled
for %%a in (%systemroot%\ipconfig.txt) do find "IP Address" <%%a>> %systemroot%\ipconfig1.txt
for /f "tokens=2 delims=:" %%b in (%systemroot%\ipconfig1.txt) do set IP=%%b
for %%c in (%systemroot%\ipconfig.txt) do find "Subnet" <%%c>> %systemroot%\ipconfig1.txt
for /f "tokens=2 delims=:" %%d in (%systemroot%\ipconfig1.txt) do set Subnet=%%d
for %%e in (%systemroot%\ipconfig.txt) do find "Default" <%%e>> %systemroot%\ipconfig1.txt
for /f "tokens=2 delims=:" %%f in (%systemroot%\ipconfig1.txt) do set Default=%%f
for /f "tokens=*" %%g in (%systemroot%\ipconfig.txt) do (
set /a N+=1
if !N! ==21 echo %%g >> %systemroot%\ipconfig1.txt
if !N! ==22 echo %%g >> %systemroot%\ipconfig1.txt
)
for /f "tokens=2 delims=:" %%h in (%systemroot%\ipconfig1.txt) do set DNS1=%%h
for /f %%i in (%systemroot%\ipconfig1.txt) do set DNS2=%%i
netsh interface ip set address local static %IP% %Subnet% %Default% 1
netsh interface ip set address local static %IP% %Subnet% %Default% 1
netsh interface ip add dns "local area connection" %DNS1%
netsh interface ip add dns "local area connection" %DNS2% index=2
CD.>%systemroot%\ipconfig1.txt
pause
shutdown -r -f -t 0 & exit



Report •

#7
June 4, 2009 at 04:47:04
Yes, that does work.

One caveat here is that all your network devices are called
"local area connection"
if for some reason you've had a nic go bad or something else
and you have a machine with "local area connection 2" or
beyond, this will not implement on those nics.

However, beyond that it works very well. Obviously this is not a very managed approach to configuring a network, but it does exactly what is advertised. It takes dynamically assigned network values and converts them to static.

A very neat little script that does the job.



Report •


Ask Question