Wednesday, September 24, 2014

Changing IP address via Batch File

I frequently have to change the IP address on my laptop to access various network devices that I'm configuring.

It wasn't so bad in Windows XP, but in Windows 7 it takes far to many clicks for something I'm doing around 10 times a day.

So I decided to make a Script I could just click to allow me to set the IP



Poking around on the internet, brought me to these two sites:
http://community.spiceworks.com/how_to/show/320-batch-file-script-to-change-ip-addresses
https://sites.google.com/site/eneerge/home/BatchGotAdmin

The first link is the basis of my script (I liked being able to just type the IP i wanted, instead of keeping different batch files for different configs.

The second one was important to make it work on Windows 7 without having to manually select "Run As Administrator"

First, make sure your Network adaptor is named LAN.  Otherwise, change all instances in the file of "LAN" to the name of your network adaptor.

I've modified this to include DNS.

Save this to notepad, and change it's extension to .bat.


  • UPDATE 9/25/2014:  Added the continue quit section at the beginning, cause sometimes I don't know what IP I have, and just clicking this on my desktop is much shorter than launcing a cmd prompt and doing an IPCONFIG, or going thru the GUI and finding the interfaces status
  • 1/5/2015:  Since I added the IP address section, when you select continue, it must restart the script, so you'll get the do you want to continue twice.  I have an idea on how to fix this, but no time to implement / test.  Later we hope (I just plan on adding a root detection at the beginning, and if it's true, skip the question)
  • 3/11/2015: Added the root detection, so now it skips the question.  I also added an option C, Static IP without DNS, since most of the time I don't need it.


@echo off
echo Check Permissions
>nul 2>&1 "%SYSTEMROOT%\system32\cacls.exe" "%SYSTEMROOT%\system32\config\system"
if '%errorlevel%' == '0' (
    echo Already have Admin
 goto :PostAdmin
)
ECHO Don't have Admin
ECHO Here are the settings for %computername%: 
netsh int ip show config LAN
echo Choose: 
echo [A] Continue
echo [B] Quit
echo.
:choice2 
SET /P C=[A,B]? 
for %%? in (A) do if /I "%C%"=="%%?" goto Continue 
for %%? in (B) do if /I "%C%"=="%%?" goto end
goto choice2

:Continue
:: BatchGotAdmin
:-------------------------------------
REM  --> Check for permissions
>nul 2>&1 "%SYSTEMROOT%\system32\cacls.exe" "%SYSTEMROOT%\system32\config\system"

REM --> If error flag set, we do not have admin.
if '%errorlevel%' NEQ '0' (
    echo Requesting administrative privileges...
    goto UACPrompt
) else ( goto gotAdmin )

:UACPrompt
    echo Set UAC = CreateObject^("Shell.Application"^) > "%temp%\getadmin.vbs"
    echo UAC.ShellExecute "%~s0", "", "", "runas", 1 >> "%temp%\getadmin.vbs"

    "%temp%\getadmin.vbs"
    exit /B

:gotAdmin
    if exist "%temp%\getadmin.vbs" ( del "%temp%\getadmin.vbs" )
    pushd "%CD%"
    CD /D "%~dp0"
:--------------------------------------
@echo off
:PostAdmin
echo Choose: 
echo [A] Set Static IP Without DNS
echo [B] Set DHCP
echo [C] Set Static IP With DNS
echo. 

:choice 
SET /P C=[A,B,C]? 
for %%? in (A) do if /I "%C%"=="%%?" goto A 
for %%? in (B) do if /I "%C%"=="%%?" goto B 
for %%? in (C) do if /I "%C%"=="%%?" goto C
goto choice 

:A 
@echo off 
echo "Please enter Static IP Address Information" 
echo "Static IP Address:" 
set /p IP_Addr=

echo "Subnet Mask:" 
set /p Sub_Mask=

echo "Default Gateway:" 
set /p D_Gate=

echo "Setting Static IP Information" 
netsh interface ip set address "LAN" static %IP_Addr% %Sub_Mask% %D_Gate% 1 
netsh int ip show config LAN
pause 
goto end

:B 
@ECHO OFF 
ECHO Resetting IP Address and Subnet Mask For DHCP 
netsh int ip set address name = "LAN" source = dhcp
netsh int ip set dns name = "LAN" source = dhcp

ipconfig /renew LAN

ECHO Here are the new settings for %computername%: 
netsh int ip show config LAN

pause 
goto end 

:C
@echo off 
echo "Please enter Static IP Address Information" 
echo "Static IP Address:" 
set /p IP_Addr=

echo "Subnet Mask:" 
set /p Sub_Mask=

echo "Default Gateway:" 
set /p D_Gate=

echo "DNS:"
set /p DNS_Addr=

echo "Setting Static IP Information" 
netsh interface ip set address "LAN" static %IP_Addr% %Sub_Mask% %D_Gate% 1 
netsh interface ipv4 set dns "LAN" static %DNS_Addr%
netsh int ip show config LAN
pause 
goto end

:end

1 comment: