Link to home
Start Free TrialLog in
Avatar of cellomania
cellomania

asked on

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?
Avatar of manav08
manav08
Flag of Australia image

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
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
************************************************************
Avatar of cellomania
cellomania

ASKER

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.

how could i put in  variables  ip address,netmask, default gw and dns?
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
************************************************************
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??
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..
ASKER CERTIFIED SOLUTION
Avatar of cellomania
cellomania

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi cellomania,

If you created the script yourself it would be good to share it with all of experts-exchange users
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