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