[[windows_wiki:batch_-_change_ip]]

Batch - Change IP Static/DHCP

General Information

Quickly change from/to static to dhcp.

Checklist

  • OS: Windows XP/7

file ip.bat
@echo off
rem Program: ip.bat
rem Author: Bill Howe
rem Version: 1.0
rem Date: 03/15/2012
rem Description: Command Line Utility to quickly set static or dhcp information
rem              on the specified network interface.
echo.
 
if "%1" == "static" (
goto staticip
)else if "%1" == "dhcp" (
goto dhcp
)else (
goto error)
 
:staticip
echo ==Static IP Config==
echo.
 
echo --- Available Interfaces ---
netsh interface show interface
set INTERFACE=Local Area Connection
set /p INTERFACE= ">>Interface Name to configure [Local Area Connection]:"
echo.
 
echo -- %INTERFACE% --
set IPADDR=192.168.1.50
set /p IPADDR= Enter IP Address[192.168.1.50]: 
set MASK=255.255.255.0
set /p MASK= Enter Subnet Mask[255.255.255.0]: 
set GW="192.168.1.1"
set /p GW= Enter Gateway[192.168.1.1]: 
set DNS=8.8.8.8
set /p DNS=Enter DNS[8.8.8.8]:
 
echo.
echo Setting interface information...
netsh interface ipv4 set address "%INTERFACE%" static %IPADDR% %MASK% %GW%
netsh interface ipv4 set dnsservers "%INTERFACE%" static %DNS%
 
rem Wait 5 seconds and then show the ipconfig of local area connection
ping -n 6 127.0.0.1 > nul
netsh interface ip show address "%INTERFACE%"
goto end
 
:dhcp
echo ==DHCP Config==
 
echo --- Available Interfaces ---
netsh interface show interface
set INTERFACE=Local Area Connection
set /p INTERFACE= ">>Interface Name to configure [Local Area Connection]:"
echo.
 
echo -- %INTERFACE% --
echo Attempting to get DHCP for %INTERFACE%...
netsh interface ipv4 set address "%INTERFACE%" dhcp
netsh interface ipv4 set dnsservers name="%INTERFACE%" source=dhcp
 
rem Wait 5 seconds and then show the ipconfig of local area connection
ping -n 6 127.0.0.1 > nul
netsh interface ip show address "%INTERFACE%"
goto end
 
:error
echo ==IP Command Help==
echo ip syntax:
echo    ip static = Invoke static IP prompts.
echo    ip dhcp = Attempt to automatically obtain an IP address.
echo.
 
:end
  • windows_wiki/batch_-_change_ip.txt
  • Last modified: 2019/05/26 03:50
  • (external edit)