Easily change IP Settings with BAT File

Hey all!

Recently I came across what seems a quite common problem. Specially if you travel or have had to connect to different networks where you need different configurations to get your job done.

As I got tired of manually changing the IP Configuration from the Network Settings window, my next goal was to create a simple script file that would allow me to do this in a very simple way.

Batch scripting can be VERY tricky, specially if you are not careful with the syntax, and you are using it as you are learning :).

So here it goes:

@ECHO OFF

set varIP=192.168.0.2
set varMK=255.255.255.0
set varGW=192.168.0.1
set vardns1=127.0.0.1
set intID="Wireless Network Connection"

ECHO Setting IP properties.

netsh int ipv4 set address %intID% static %varIP% %varMK% %varGW% 1

ECHO IP Properties set.
ECHO Setting DNS Properties

netsh int ipv4 set dns %intID% static %vardns1%

pause

Easy.

Now, this solves the issue, but unfortunately I needed a couple more files for different tests, so I decided to write something a bit longer (and tidier).

First, I want to know what interfaces are active, and easily be able to choose them. Hence, the following:

@ECHO OFF

ECHO Choose the interface (input the id):

setlocal EnableDelayedExpansion

FOR /F "tokens=1,5,*" %%A IN (‘netsh int ipv4 show interfaces’) DO (
SET initString=%%A

REM These next three lines just format the output table nicely,
REM they will be explained later.
call :strlen resLen !initString!
call :getNiceString resultString !resLen! 5 !initString!
ECHO !resultString! %%B %%C
)

REM Get the input from the user
set /P intID=Type interface ID: %=%

And the output nicely put:

The different interfaces available and their IDs

Note in the code above that the FOR loop goes through all each line of what comes out of the ‘netsh int ipv4 show interfaces’ command, and this gives a lot of unwanted information.
I just want the first and fifth columns, which is why I select the 1st, the 5th and whatever comes after that (in case an interface has a name consisting of 1+ words). Note that by default, ” ” (space) is taken as the delimiter.

Now that I have the interface, I want to know whether to choose the default DHCP configuration, or whether I need a specific profile:

[bat firstline=”19″] ECHO Do you want DHCP? (y/n)

set /P answer=Type answer: %=%

if "%answer%"=="y" (

REM configure IP and DNS for the given DHCP
netsh int ipv4 set address %intID% dhcp
netsh int ipv4 set dns %intID% dhcp

) ELSE (
ECHO Choose your configuration file:
set /a var=1
REM list the files in the directory which contain ".bat" in their names
REM and save them in an array (there are no arrays in batch scripting), explained later.
FOR /F %%F IN (‘DIR /B /oN^|FIND /i "bat"’) DO (
REM check that the file being listed is not the running file
set varF=false
if %%F == %0 set varF=true
if /I "%%F" EQU "%0.bat"; set varF=true
if !varF!==false (
set __Files.!var! = %%F
ECHO !var! – %%F
set /a var = !var!+1
)
)
REM Ask for the file number
set /P file=Type file number: %=%

REM this might seem confusing at first. The SET __Files.X command
REM where X is a number, returns a line similar to this:
REM __Files.X = some_value. And because "some_value" is what matters,
REM we need to split that line in two tokens. How to do it:
FOR /F "tokens=2* delims=.=" %%A IN (‘SET __Files.!file!’) DO (
ECHO Starting profile %%B
START %%B %intID%
ECHO Profile %%B succesfully started.
)
)
endlocal
pause
goto :eof
[/bat]

Finally, if you check out lines 12 and 13 in the second block of code (“Getting the available interfaces”), the code for those function calls is the following:

[bat firstline=”62″ title=”Printing a table nicely” collapse=”1″] REM the following portion of code is here just to print things nicely
REM gets the necessary number of spaces to add to a string and returns
REM the value in the first parameter.
:getNiceString <resultString> <strLength> <resultLength> <initString>
(
set /a diff = %~3 – %~2
set resString=%~4
FOR /L %%i in (1,1,!diff!) DO (
REM note the space at the end of the following line
set resString=!resString!
REM right here ^
)
set "%~1=!resString!"
exit /b
)

REM gets the length of a string variable
:strlen <resultVar> <stringVar>
(
set string=%~2
set len=0
:loop
if "!string!" NEQ "" (
set string=!string:~1!
set /a len=!len!+1
goto loop
)
)
(
set "%~1=%len%"
exit /b
)
[/bat]

Done!
Now all you need is to put the main file in a folder along with the profile files that you want, and then just select the profile of your choice.
Hope you found this useful!

The main file:

IPSetup

Sample profiles:

Home

Office

3 comments

Leave a Reply

Your email address will not be published. Required fields are marked *