Computing.Net > Forums > Programming > grab DHCP IP and set as static

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

grab DHCP IP and set as static

Reply to Message Icon

Name: aslindj
Date: April 1, 2009 at 06:01:12 Pacific
OS: XP
CPU/Ram: various
Product: Dell / VARIOUS
Subcategory: Batch
Comment:

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



Sponsored Link
Ads by Google

Response Number 1
Name: Shaka
Date: April 2, 2009 at 01:48:24 Pacific
Reply:

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


0

Response Number 2
Name: aslindj
Date: April 2, 2009 at 07:08:57 Pacific
Reply:

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!


0

Response Number 3
Name: Shaka
Date: April 2, 2009 at 10:55:44 Pacific
Reply:

You are most welcome. Glad to have helped. Obviously my dns servers do not change.
Good luck to you.


0

Response Number 4
Name: FishMonger
Date: April 2, 2009 at 12:02:22 Pacific
Reply:

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...


0

Response Number 5
Name: supertech
Date: June 3, 2009 at 12:08:10 Pacific
Reply:

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?


0

Related Posts

See More



Response Number 6
Name: Shaka
Date: June 4, 2009 at 03:06:20 Pacific
Reply:

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



0

Response Number 7
Name: aslindj
Date: June 4, 2009 at 04:47:04 Pacific
Reply:

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.



0

Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: grab DHCP IP and set as static

Creating Network Shares and Setting Perm www.computing.net/answers/programming/creating-network-shares-and-setting-perm/187.html

find, replace, and save as www.computing.net/answers/programming/find-replace-and-save-as/16885.html

Create folder in desktop and set disk quota www.computing.net/answers/programming/create-folder-in-desktop-and-set-disk-quota/19212.html