Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

CMD to change IP address 2

Status
Not open for further replies.

dbustamante

IS-IT--Management
Jul 12, 2010
63
US
ok guys ive been trying to come up with a command that will change the ip and dns via .cmd the only problem is my syntex is wrong and i cant figure out where i messed it up maybe you all can help me.

@echo off

netsh interface ip set addresss "Local Area Connection" static 192.168.1.101 255.255.255.0 192.168.1.1 1
index=1 192.0.0.52
index=2 192.0.0.69


 
Thats it you hit the nail on the head. Thanks for the quick Responce. :)
 
Hey, this is a bit late, but we've been using this little batch file (setip.bat) on our systems for quite a while.

You can call it directly from the command-line by specifying the network adapter to be changed. Just drop it in System32. For what it's worth!

Code:
:: Purpose:			Allows you to quickly change your IP address without using the GUI
:: Requirements:	Windows XP or Vista
:: History:			3.1 Removed warning, fixed error when deleting temp file
::					3.0 Major re-write:
::						-- Script now supports command-line invocation, with [optional] name of adapter to be changed
::						-- Combined both scripts into one, and added warning to the beginning
::						-- Script can now be invoked with adapter name to be changed. If not it will prompt for it.
::						-- Re-wrote entire menu structure and added new options (DHCP, Quit)
::						-- Cleaned up code to use %TEMP% variable for storing the working text files instead of C:\
::						-- Script now offers to use the OpenDNS servers as backup to the default gateway (for DNS)
::					2.0 Changed hard-coded adapter name to a script-wide variable and cleaned up menus
::					1.5 Created basic (ugly) menu structure
::					1.0 Initial write

::::::::::::::::::::::::::::::::::::::::::::
:: Initialize variables & check arguments ::
::::::::::::::::::::::::::::::::::::::::::::
:prep
:: \/ The first argument passed to the script becomes the adapter to be changed. \/
set ADAPTER=%1
set IP=
set GATEWAY=
set MASK=
set DNS1=
set DNS2=208.67.222.220
set DNS3=208.67.220.222
:: Did the user pass an argument upon invocation? If not, then ask them for the adapter to be 
:: changed before going on to the setip portion.
if '%1%'=='' goto specify_adapter
		else goto start

:: Main Screen
:start
color 07 & title TCP/IP Configuration for LAN & cls
ipconfig /all > "%TEMP%\tempLANconfig.txt"
findstr /R "IPv4 IPv6 Subnet DHCP Dhcp IP.A Default Ethernet DNS.Ser" <"%TEMP%\tempLANconfig.txt" >"%TEMP%\tempLANconfig2.txt"
echo.
echo ------------------------------
echo  Current TCP/IP Configuration
echo ------------------------------
echo.
type "%TEMP%\tempLANconfig2.txt"
echo. 
echo. 
echo    Adapter to be changed is: %ADAPTER%
echo    -----------------------------------
echo    Enter "1" to change the IP address manually.
echo    Enter "2" to set the adapter to DHCP and attempt to acquire an address.
echo    Enter "3" to pick a different adapter to change.
echo    Enter "4" to abort and quit.
:menu
set choice=
echo.
set /p choice=Choice: 
if not '%choice%'=='' set choice=%choice:~0,1%
	if '%choice%'=='1' goto enter_values
	if '%choice%'=='2' goto dhcp
	if '%choice%'=='3' goto specify_adapter
	if '%choice%'=='4' goto cancel
echo.
echo  "%choice%" is not valid, please try again
echo.
goto menu 
pause

:: Specify Adapter :: - Only called if the user didn't pass an adapter name as the first argument
:specify_adapter
cls
color 07 & title IP Address Changer & cls
title TCP/IP Configuration for LAN
ipconfig /all > "%TEMP%\tempLANconfig.txt"
findstr /R "IPv4 IPv6 Subnet DHCP Dhcp IP.A Default Ethernet DNS.Ser" <"%TEMP%\tempLANconfig.txt" >"%TEMP%\tempLANconfig2.txt"
echo.
echo ------------------------------
echo  Current TCP/IP Configuration
echo ------------------------------
echo.
type "%TEMP%\tempLANconfig2.txt"
echo.
echo  ************************************************************
echo  You didn't specify an adapter to be changed! To save time,
echo  invoke the script with the name of the adapter you want 
echo  changed as an argument. 
echo.
echo  Examples:  setip "Local Area Connection 1"
echo             setip Wireless
echo  ************************************************************
echo  When setting the adapter here, always use quotes around the 
echo  name if it contains spaces. Also be sure to use proper case.
echo.
set /p ADAPTER=Enter the name of the ADAPTER to be changed: 
goto start

:enter_values
echo.
echo.
set /p IP=Enter the new IP address:         
set /p MASK=Enter the new subnet mask:        
set /p GATEWAY=Enter the new default gateway:    
	set DNS1=%GATEWAY%
	
echo.
echo These default DNS servers will be loaded:
echo Primary:   %GATEWAY% (default gateway)
echo Secondary: %DNS2% (OpenDNS)
echo Tertiary:  %DNS3% (OpenDNS)
echo.
	set /p CHOICE=Is this okay? [Y/n]: 
	if '%CHOICE%'=='y' goto execute_prompt
	if '%CHOICE%'=='n' goto enter_dns

:execute_prompt
color f0
cls
echo.
echo About to apply this configuration:
echo.
echo IP Address:      %IP%
echo Subnet Mask:     %MASK%
echo Default Gateway: %GATEWAY%
echo DNS Servers:     %DNS1% (Primary)
echo                  %DNS2%
echo                  %DNS3%
echo.
set /p CHOICE=Apply this configuration? [Y/n]: 
	if '%CHOICE%'=='y' goto execute_do
	if '%CHOICE%'=='n' goto start

:execute_do
color 07 & cls
echo.
echo Applying the TCP/IP configuration.
echo This could take up to 30 seconds, please be patient...
echo.
echo Setting IP address to %IP%...
	netsh interface ip set address name=%ADAPTER% source=static %IP% %MASK% %GATEWAY% 1
echo Setting the default gateway %GATEWAY% as primary DNS...
	netsh interface ip set dns name=%ADAPTER% static %DNS1% primary
echo Setting %DNS2% and %DNS3% as the alternate DNS servers...
	netsh interface ip add dns name=%ADAPTER% %DNS2% index=2
	netsh interface ip add dns name=%ADAPTER% %DNS3% index=3
goto done

:dhcp
echo.
echo Using DHCP to acquire address...
echo.
netsh interface ip set address name=%ADAPTER% source=dhcp
echo Using DHCP to acquire DNS servers...
netsh interface ip set dns name=%ADAPTER% source=dhcp
ipconfig /renew
goto done


:: Enter DNS
:: This section is just in case the user isn't okay with default DNS settings. This will be skipped most of the time.

:enter_dns
set /p DNS1=Enter IP address of first DNS server: 
set /p DNS2=Enter IP address of second DNS server: 
set /p DNS3=Enter IP address of third DNS server: 
goto execute_prompt

:done
cls & color f0
echo.
:: Show user the results. We output IP config information to a file, strip out some stuff, then present the results. Finally, we delete the temp file. 
echo   Results:
echo   --------
ipconfig /all > "%TEMP%\tempLANconfig.txt"
findstr /R "IPv4 IPv6 Subnet DHCP Dhcp IP.A Default Ethernet DNS.Ser" <"%TEMP%\tempLANconfig.txt" >"%TEMP%\tempLANconfig2.txt"
echo.
type "%TEMP%\tempLANconfig2.txt"
echo.
echo.
pause
del "%TEMP%\*config*txt" /Q
del "%TEMP%\tempLANconfig.txt" /Q
del "%TEMP%\tempLANconfig2.txt" /Q
exit

:cancel
del "%TEMP%\*config*txt" /Q
cls
echo.
echo Canceled! Goodbye.

 
better late then never its always good to have a back up just in case Thanks for the help.
 
i started playing around with your script and it works really well. would you happen to know how to add a feature that lets you change the PC name as well as the ip address even if the PC is on a domain?
 
Hi dbustamante, glad the script is working for you!

I googled around a bit and the only thing I found was this:

Apparently there's a utility called "netdom.exe" that's part of the Windows XP support tools that will do what you want. It'd be pretty easy to add a line or two in the batch file to call NetDom and change the name too I think.
 
hagate73,

quick response and its exactly what i needed Thanks for all the help i appreciate it.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top