Link to home
Start Free TrialLog in
Avatar of amaru96
amaru96

asked on

Conditional based on current IP

Hi guys, I'm after a batch script which will check the current IP of the machine and if it falls within a list of certain ranges, go to another section of the script.

So something like this:
IPrange1 = 1.1.1.x
IPrange2 = 1.1.2.x
IPrange3 = 1.1.3.x
If current IP address is in IPrange1 goto range1
If current IP address is in IPrange2 goto range2
If current IP address is in IPrange3 goto range3
.....
Avatar of Steve Knight
Steve Knight
Flag of United Kingdom of Great Britain and Northern Ireland image

On phone at mo. so can't type it out here but this is pretty well what a script of mine here does:

 http://scripts.dragon-it.co.uk/links/batch-get-tcpip-subnet

have a look and can help customise if needed.

steve
ASKER CERTIFIED SOLUTION
Avatar of oBdA
oBdA

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
The proper construct should look something like this:

@echo off

for /f "tokens=2 delims=:" %%a in ('ipconfig ^| find "IPv4"') do (
  for /f "tokens=3 usebackq delims=." %%b in ('%%a') do goto range%%b
)

exit /b

:range1
  echo Range 1
goto :eof

:range2
  echo Range 2
goto :eof

:range3
  echo Range 3
goto :eof

:etc...

Open in new window

Nice oBdA..

An easy quick way if in a specific environment is to check simply using ipconfig, i.e.

ipconfig | find "1.1.2." && echo I am on subnet with 1.1.2. in it

paul - works OK unless on pre-IPv6 machine like XP though.

There may also be other better ways depending upon WHY you want this, such as using DNS to provide an IP in the same subnet based on the same name with multiple records, using DFS for a drive mapped to local server etc.

Steve
Oops! I meant...

@echo off

for /f "tokens=2 delims=:" %%a in ('ipconfig ^| find "IPv4"') do (
  for /f "tokens=3 delims=." %%b in ('echo %%a') do goto range%%b
)

exit /b


:range1
echo Range 1
goto :eof

:range2
echo Range 2
goto :eof

:range3
echo Range 3
goto :eof

:etc...

Open in new window

Avatar of amaru96
amaru96

ASKER

Thanks guys, appreciate the help. Works really well.
Well done oBdA, you put a lot of effort into that...