We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

Windows script to fix a dhcp ip address settin

cellomania
cellomania asked
on
Medium Priority
339 Views
Last Modified: 2012-05-06
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?
Comment
Watch Question

Commented:
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

Commented:
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
************************************************************

Author

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.

Author

Commented:
how could i put in  variables  ip address,netmask, default gw and dns?

Commented:
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
************************************************************

Commented:
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??

Commented:
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..
Hi,
I created the script myself :)
Thanks anyway

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Commented:
Hi cellomania,

If you created the script yourself it would be good to share it with all of experts-exchange users

Author

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

Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.