• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 889
  • Last Modified:

Create a bat file to switch between a static and dhcp

Hi,

I want a bat file to switch between my network which pushes the DHCP ip address and a static ip when i connect to a different domain.The ip for static is 10.10.1.1

Any way to get this done in 1 bat file.

regards
Sharath
0
bsharath
Asked:
bsharath
  • 6
  • 6
  • 3
  • +1
1 Solution
 
fetch_Commented:
Have a look at: http://www.petri.co.il/configure_tcp_ip_from_cmd.htm

also, you could install another NIC and enable/disable them as needed.  
0
 
Admin4XPCommented:
You could use the NETSH command i.e

netsh interface ip set address "Local Area Connection" dhcp

OR

netsh interface ip set address name="Local Area Connection" static 10.10.1.1

Check it out here:
http://www.petri.co.il/configure_tcp_ip_from_cmd.htm
0
 
Admin4XPCommented:
LOL sorry Fetch didnt refresh before i posted :S
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
bsharathAuthor Commented:
can u help me put this in a bat file?
0
 
canaliCommented:
I need netmask, gateway, dns
switching between static ->dhcp ->static these parameters are lost

Gas
0
 
bsharathAuthor Commented:
I JUST NEED TO CHANGE THE IP AND SUBNET
0
 
Admin4XPCommented:
To change the IP and subnet just type

netsh interface ip set address name="Local Area Connection" static 10.10.1.1 255.255.255.0

You can save it in notepad and call it Static.bat if you want to double click it to run. The same can be done to go back to DHCP

netsh interface ip set address "Local Area Connection" dhcp
0
 
canaliCommented:
@echo off
setlocal
:: *** change your parameter
set domDHCP=WEBDOM
set domSTATIC=HOMEDOM
:: *** change your IP settings
set ADDRESS=10.10.1.1
set NETMASK=255.255.0.0
set GATEWAY=10.10.254.254
set DNS=10.10.100.100
set WINS1=10.10.100.1
set WINS1=10.10.100.2
:: *************************
if /i +%USERDOMAIN%+==+%domSTATIC%+ call :_STATIC
if /i +%USERDOMAIN%+==+%domDHCP%+ call :_DHCP
netsh exec %temp%\ip.tmp
ipconfig
pause
goto :_END

:_DHCP
if exist %temp%\ip.tmp  del %temp%\ip.tmp
set w=echo ^>^>%temp%\ip.tmp
%w% # ----------------------------------
%w% # Interface IP Configuration        
%w% # ----------------------------------
%w% pushd interface ip
%w% # Interface IP Configuration for "Local Area Connection"
%w% set address name="Local Area Connection" source=dhcp
%w% set dns name="Local Area Connection" source=dhcp register=PRIMARY
%w% set wins name="Local Area Connection" source=dhcp
%w% popd
%w% # End of interface IP configuration
goto :_EOF

:_STATIC
if exist %temp%\ip.tmp  del %temp%\ip.tmp
set w=echo ^>^>%temp%\ip.tmp
%w% pushd interface ip
%w% # Interface IP Configuration for ""Local Area Connection""
%w% set address name="Local Area Connection" source=static addr=%ADDRESS% mask=%NETMASK%
%w% set address name="Local Area Connection" gateway=%GATEWAY% gwmetric=0
%w% set dns name="Local Area Connection" source=static addr=%ADDRESS% register=PRIMARY
%w% add dns name="Local Area Connection" addr=%DNS%
%w% set wins name="Local Area Connection" source=static addr=%WINS1%
%w% add wins name="Local Area Connection" addr=%WINS"%
%w% # Interface IP Configuration for "Local Area Connection"
%w% popd
%w% # End of interface IP configuration
goto :_EOF

:_END
:_EOF


0
 
fetch_Commented:
If you're not familiar with .bat scripting.  Just write 2 bat files.  Put 1 line in each.


netsh interface ip set address "Local Area Connection" static ipaddr subnetmask gateway metric

netsh interface ip set address "Local Area Connection" dhcp

from: http://support.microsoft.com/?kbid=257748
0
 
canaliCommented:
save the batch as changeip.cmd, and simply double click it:
if you are in WEBDOM  then dhcp is setted
if you are in HOMEDOM then static ip is setted

Bye Gas
0
 
bsharathAuthor Commented:
canali.

It gives this.

Windows cannot open the file named C:\DOCUME~1\sharathr\LOCALS~1\Temp\ip.tmp.
The system cannot find the file specified.

0
 
bsharathAuthor Commented:
fetch

I get this


C:\Documents and Settings\sharathr\Desktop>netsh interface ip set address "Local
 Area Connection" 10.0.0.10 255.0.0.0
10.0.0.10 is not an acceptable value for source.

0
 
canaliCommented:
Fill correcly  domDHCP and domSTATIC
look at the command prompt the  domain name
c:\>echo %USERDOMAIN%

Bye Gastone Canali


@echo off
setlocal
:: *** change your parameter
set domDHCP=WEBDOM
set domSTATIC=BSHARATHDOM
:: *** change your STATIC IP settings
set ADDRESS=10.10.1.1
set NETMASK=255.255.0.0
set GATEWAY=10.10.254.254
set DNS=10.10.100.100
set WINS1=10.10.100.1
set WINS1=10.10.100.2
:: *************************
if /i +%USERDOMAIN%+==+%domSTATIC%+ call :_STATIC
if /i +%USERDOMAIN%+==+%domDHCP%+ call :_DHCP
if not exist %temp%\ip.tmp  echo ERROR DOMAIN NOT FOUND domSTATIC or domDHCP & goto :_END
netsh exec %temp%\ip.tmp
ipconfig
goto :_END

:_DHCP
if exist %temp%\ip.tmp  del %temp%\ip.tmp
set w=echo ^>^>%temp%\ip.tmp
%w% # ----------------------------------
%w% # Interface IP Configuration        
%w% # ----------------------------------
%w% pushd interface ip
%w% # Interface IP Configuration for "Local Area Connection"
%w% set address name="Local Area Connection" source=dhcp
%w% set dns name="Local Area Connection" source=dhcp register=PRIMARY
%w% set wins name="Local Area Connection" source=dhcp
%w% popd
%w% # End of interface IP configuration
goto :_EOF

:_STATIC
if exist %temp%\ip.tmp  del %temp%\ip.tmp
set w=echo ^>^>%temp%\ip.tmp
%w% pushd interface ip
%w% # Interface IP Configuration for ""Local Area Connection""
%w% set address name="Local Area Connection" source=static addr=%ADDRESS% mask=%NETMASK%
%w% set address name="Local Area Connection" gateway=%GATEWAY% gwmetric=0
%w% set dns name="Local Area Connection" source=static addr=%ADDRESS% register=PRIMARY
%w% add dns name="Local Area Connection" addr=%DNS%
%w% set wins name="Local Area Connection" source=static addr=%WINS1%
%w% add wins name="Local Area Connection" addr=%WINS"%
%w% # Interface IP Configuration for "Local Area Connection"
%w% popd
%w% # End of interface IP configuration
goto :_EOF

:_END
:_EOF
0
 
bsharathAuthor Commented:
Canali...What are the things i need to change excluding the ip's

What should i change here

set domDHCP=WEBDOM
set domSTATIC=BSHARATHDOM
0
 
canaliCommented:
I'm assuming you have 2 domains one is WEBDOM where you need to set dhcp, the other is BSHARATHDOM with a static ip ... probably I don't understand your question.
This batch ask what you want: Static or dhcp? [s,d]

@echo off
setlocal
:: *** Ask for a choice
set /p iptype=Static or dhcp? [s,d]
:: *** change your STATIC IP settings
set ADDRESS=10.10.1.1
set NETMASK=255.255.0.0
set GATEWAY=10.10.254.254
set DNS=10.10.100.100
set WINS1=10.10.100.1
set WINS1=10.10.100.2
:: *************************
if /i +%iptype%+==+S+ call :_STATIC
if /i +%iptype%+==+D+ call :_DHCP
if /i +%iptype%+==++ echo Nothing changed & goto :_END

netsh exec %temp%\ip.tmp
ipconfig
goto :_END

:_DHCP
if exist %temp%\ip.tmp  del %temp%\ip.tmp
set w=echo ^>^>%temp%\ip.tmp
%w% # ----------------------------------
%w% # Interface IP Configuration        
%w% # ----------------------------------
%w% pushd interface ip
%w% # Interface IP Configuration for "Local Area Connection"
%w% set address name="Local Area Connection" source=dhcp
%w% set dns name="Local Area Connection" source=dhcp register=PRIMARY
%w% set wins name="Local Area Connection" source=dhcp
%w% popd
%w% # End of interface IP configuration
goto :_EOF

:_STATIC
if exist %temp%\ip.tmp  del %temp%\ip.tmp
set w=echo ^>^>%temp%\ip.tmp
%w% pushd interface ip
%w% # Interface IP Configuration for ""Local Area Connection""
%w% set address name="Local Area Connection" source=static addr=%ADDRESS% mask=%NETMASK%
%w% set address name="Local Area Connection" gateway=%GATEWAY% gwmetric=0
%w% set dns name="Local Area Connection" source=static addr=%ADDRESS% register=PRIMARY
%w% add dns name="Local Area Connection" addr=%DNS%
%w% set wins name="Local Area Connection" source=static addr=%WINS1%
%w% add wins name="Local Area Connection" addr=%WINS"%
%w% # Interface IP Configuration for "Local Area Connection"
%w% popd
%w% # End of interface IP configuration
goto :_EOF

:_END
:_EOF
0
 
bsharathAuthor Commented:
Canali.I saved this file as staticordhcp.bat then just double clicked.There is no changes or any results.
0
 
canaliCommented:
Save this script as  staticordhcp.cmd double click, answer with s or d  an all changes will be for applied to the interface "Local Area Connection". If the interface name is different from default  change the line in the batch
Ex.: set NIC-NAME=My Wireless Nettwork

@echo off
::ver 1.1
:: check the interface name
setlocal
cls

:: *** change your STATIC IP settings
set ADDRESS=10.10.1.1
set NETMASK=255.255.0.0
set GATEWAY=10.10.254.254
set DNS=10.10.100.100
set WINS=10.10.100.1
set WINS1=10.10.100.2
::*** fill the correct interface name
set NIC-NAME=Local Area Connection
:: *************************

echo.
echo Interface Name: "%NIC-NAME%"
echo.
:: *** Ask for a choice
set /p iptype=Static or dhcp? [s,d]
(ipconfig |find /i "%NIC-NAME%">nul)||goto :_ERR
if /i +%iptype%+==+S+ call :_STATIC
if /i +%iptype%+==+D+ call :_DHCP
if /i +%iptype%+==++ echo Nothing changed & goto :_END

netsh exec %temp%\ip.tmp
ipconfig
pause
goto :_END

:_DHCP
if exist %temp%\ip.tmp  del %temp%\ip.tmp
Echo DHCP setted on the interface "%NIC-NAME%"
set w=echo ^>^>%temp%\ip.tmp
%w% # ----------------------------------
%w% # Interface IP Configuration        
%w% # ----------------------------------
%w% pushd interface ip
%w% # Interface IP Configuration for "%NIC-NAME%"
%w% set address name="%NIC-NAME%" source=dhcp
%w% set dns name="%NIC-NAME%" source=dhcp register=PRIMARY
%w% set wins name="%NIC-NAME%" source=dhcp
%w% popd
%w% # End of interface IP configuration
goto :_EOF

:_STATIC
if exist %temp%\ip.tmp  del %temp%\ip.tmp
Echo Static address setted on the interface "%NIC-NAME%"
set w=echo ^>^>%temp%\ip.tmp
%w% pushd interface ip
%w% # Interface IP Configuration for ""%NIC-NAME%""
%w% set address name="%NIC-NAME%" source=static addr=%ADDRESS% mask=%NETMASK%
%w% set address name="%NIC-NAME%" gateway=%GATEWAY% gwmetric=0
%w% set dns name="%NIC-NAME%" source=static addr=%ADDRESS% register=PRIMARY
%w% add dns name="%NIC-NAME%" addr=%DNS%
%w% set wins name="%NIC-NAME%" source=static addr=%WINS%
%w% add wins name="%NIC-NAME%" addr=%WINS1%
%w% # Interface IP Configuration for "%NIC-NAME%"
%w% popd
%w% # End of interface IP configuration
goto :_EOF
:_ERR
echo ERROR: Network interface "%NIC-NAME%" not found!

:_END
:_EOF
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 6
  • 6
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now