Windows script to fix a dhcp ip address settin

Hi,
need a script executable in windows xp that check the ip address, subnet mask, default gateway and DNS assigned by DHCP, and make those fixed in the network adapter setting.

I need also a script that enable again the DHCP when executed.
Is it possible?
cellomaniaAsked:
Who is Participating?
 
cellomaniaAuthor Commented:
Hi,
I created the script myself :)
Thanks anyway
0
 
manav08Commented:
Why don't you setup something like http://articles.techrepublic.com.com/5100-10878_11-5611546.html
Setup an IP ADDRESS reservation and delete it when you wanna switch it back truly to DHCP.

To get the MAC ADDRESS of the machine you are trying to put a reservation for do the following on the server -

1. PING IT
2. go to command prompt and type in arp-a
0
 
manav08Commented:
sorry it was "arp -a" with a space.
You can do all this via a script too but it would be comlicated and more work.

SCRIPT to setup static IP ADDRESS is -

***************************************************
netsh interface ip set address name = "Local Area Connection" source = static addr = 10.0.0.99 mask = 255.255.255.0
netsh interface ip set address name = "Local Area Connection" gateway = 10.0.0.254 gwmetric = 1
netsh interface ip set dns name = "Local Area Connection" source = static addr = 24.222.0.1
netsh interface ip add dns name = "Local Area Connection" addr = 24.222.0.2

*************************************************

Script to revert settings back to DHCP -


***********************************************************
rem Reset network settings for DHCP set
netsh interface ip set address name = "Local Area Connection" source = dhcp
netsh interface ip set dns name = "Local Area Connection" source = dhcp
netsh interface ip set wins name = "Local Area Connection" source = dhcp
************************************************************
0
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

 
cellomaniaAuthor Commented:
Hi,
I can't change the dhcp setting.
I need to fix the ip address because when I activate VPN, my ip address expire in dhcp and it can't be renewed because VPN is up.
This dhcp has 60 seconds of expiration time.

0
 
cellomaniaAuthor Commented:
how could i put in  variables  ip address,netmask, default gw and dns?
0
 
manav08Commented:
BATCH SCRIPT to setup static IP ADDRESS is -

set_static.bat
***************************************************
netsh interface ip set address name = "Local Area Connection" source = static addr = 10.0.0.99 mask = 255.255.255.0
netsh interface ip set address name = "Local Area Connection" gateway = 10.0.0.254 gwmetric = 1
netsh interface ip set dns name = "Local Area Connection" source = static addr = 24.222.0.1
netsh interface ip add dns name = "Local Area Connection" addr = 24.222.0.2

*************************************************

Script to revert settings back to DHCP -

set_dhcp.bat
***********************************************************
rem Reset network settings for DHCP set
netsh interface ip set address name = "Local Area Connection" source = dhcp
netsh interface ip set dns name = "Local Area Connection" source = dhcp
netsh interface ip set wins name = "Local Area Connection" source = dhcp
************************************************************
0
 
manav08Commented:
The above script will only set static IP but will not check the IP etc. for you.
This is going to require more time and wil be very complex to do.
What I do not understand is that even if DHCP expires in 60 seconds why can you not change it??
0
 
manav08Commented:
I can write a program like this for you but the points for this question then should be 500 atleast. It will just mean that I do your work ;-). It will take me an hour or 2 to write it for you..
0
 
manav08Commented:
Hi cellomania,

If you created the script yourself it would be good to share it with all of experts-exchange users
0
 
cellomaniaAuthor Commented:
Here the scripts:
@Echo off
 
ipconfig |find "IP Address" > %temp%\Ipaddr.txt
for /f "tokens=*" %%a in (%temp%\ipaddr.txt) do set ipaddr=%%a & goto :next
:next
 
Rem Get end of string  replace    with nothing
set ipaddr=%ipaddr:. =%
 
REM Remove IP Address:
set ipaddr=%ipaddr:IP Address:=%
 
REM Remove any spaces to leave IP address xxxx
set ipaddr=%ipaddr: =%
 
echo Your IP address is %ipaddr%
 
ipconfig |find "Subnet Mask" > %temp%\Ipaddr.txt
for /f "tokens=*" %%a in (%temp%\ipaddr.txt) do set mask=%%a & goto :next
:next
 
Rem Get end of string  replace    with nothing
set mask=%mask:. =%
 
 
REM Remove IP Address:
set mask=%mask:Subnet Mask :=%
 
REM Remove any spaces to leave IP address xxxx
set mask=%mask: =%
 
echo SUBNET MASK %mask%
 
ipconfig |find "Default Gateway" > %temp%\Ipaddr.txt
for /f "tokens=*" %%a in (%temp%\ipaddr.txt) do set gateway=%%a & goto :next
:next
 
Rem Get end of string  replace    with nothing
set gateway=%gateway:. =%
 
REM Remove IP Address:
set gateway=%gateway:Default Gateway :=%
 
REM Remove any spaces to leave IP address xxxx
set gateway=%gateway: =%
 
echo DEFAULT GW %gateway%
 
ipconfig /all |find "DNS Servers" > %temp%\Ipaddr.txt
for /f "tokens=*" %%a in (%temp%\ipaddr.txt) do set dns=%%a & goto :next
:next
 
Rem Get end of string  replace    with nothing
set dns=%dns:. =%
 
REM Remove IP Address:
set dns=%dns:DNS Servers :=%
 
REM Remove any spaces to leave IP address xxxx
set dns=%dns: =%
echo DNS %dns%
 
 
choice /c:YN Confirm?
 
		if errorlevel 2 goto Nok
    if errorlevel 1 goto Ok
    
 
goto End
:Ok
echo Configuring..... wait 3 OK...
 
 
netsh interface ip set address name = "Local Area Connection" source = static addr = %ipaddr% mask = %mask%
netsh interface ip set address name = "Local Area Connection" gateway = %gateway% gwmetric = 1
rem netsh interface ip set dns name = "Local Area Connection" source = static addr = 24.222.0.1
netsh interface ip add dns name = "Local Area Connection" addr = %dns%
 
ipconfig /all
goto End
 
:Nok
echo NOK....
goto End
 
:End

Open in new window

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.