Batch file to compare pc ip address to string

I am attempting to find out if the ip address of the computer matches a certain subnet so I can perform specific actions.

I am using the information from here:

http://stackoverflow.com/questions/5491383/find-out-whether-an-environment-variable-contains-a-substring

My problem is getting the ip address and matching it against a string of the first three octets. 192.168.1 vs 192.168.2

Can you provide example for this? if first three octets = 192168.1 do something else do something different sort of logic?
LVL 1
zequestionerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

oBdACommented:
Try the script below; it uses WMI to query for the IP addresses, so neither different OS versions nor localized OS languages should make a difference.
The script doesn't just tests the octets, it calculates the real subnet, so even subnets like 192.168.0.0/255.255.255.128 should be processed correctly. Furthermore, it checks all IP enabled NICs, so machines with more than 1 adapter should be processed correctly as well.
@echo off
setlocal enabledelayedexpansion

REM *** Add common code here

call :GetIPConfig
call :CheckSubnet 192.168.1.0 && goto Range1
call :CheckSubnet 192.168.2.0 && goto Range1
echo No supported IP range found.
goto :eof

:Range1
echo You're in range 1
REM *** Add your code for range 1 here ...
goto :eof

:Range2
echo You're in range 2
REM *** Add your code for range 2 here ...
goto :eof

REM ====================================================================================================
REM Only functions below this line!
REM ====================================================================================================
goto :eof
:GetIPConfig
set /a NicCount = 0
for /f "skip=1 tokens=2,4 delims={}" %%a in ('wmic.exe nicconfig where "IPEnabled=true" get Caption^,IPAddress^,IPSubnet') do (
	set /a NicCount += 1
	for /f "tokens=1 delims=, " %%i in ("%%a") do (
		set IPv4[!NicCount!]=%%~i
		for /f "tokens=1-4 delims=." %%s in ("%%~i") do (
			set sn1=%%s
			set sn2=%%t
			set sn3=%%u
			set sn4=%%v
		)
	)
	for /f "tokens=1 delims=, " %%i in ("%%b") do (
		set NMv4[!NicCount!]=%%~i
		for /f "tokens=1-4 delims=." %%s in ("%%~i") do (
		  set /a sn1 "&=" %%s
		  set /a sn2 "&=" %%t
		  set /a sn3 "&=" %%u
		  set /a sn4 "&=" %%v
		)
	)
	set SNv4[!NicCount!]=!sn1!.!sn2!.!sn3!.!sn4!
)
for /l %%i in (1, 1, %NicCount%) do (
	echo Found IP Address !IPv4[%%i]!, Netmask !NMv4[%%i]!, Subnet !SNv4[%%i]!
)
goto :eof
:CheckSubnet
set Subnet=%~1
set Return=1
for /l %%i in (1, 1, %NicCount%) do (
	if %Subnet%==!SNv4[%%i]! (set Return=0)
)
exit /b %Return%

Open in new window

0
zequestionerAuthor Commented:
Thank you much. This works but it seems a bit extreme. I already know the ip address range and there is only /24. there is only 1 nic too. I would like somethink much eeasier to read but this will work for me. thank you again very much.

i do not know the right code syntax but here is what i was trying to do:

set ipsubnet=192.168.1

if %ipsubnet% == 192.168.1 (
     Echo . install.exe
) else (
     goto end
)

:end

but I do not know how to match if the ip address matches the first three octets. since ip subnet with the ipsubnet vairable.

thank you agian.
0
Steve KnightIT ConsultancyCommented:
http://scripts.dragon-it.co.uk/links/batch-get-tcpip-subnet

On mobile at mo. But see my script here.

Steve
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

zequestionerAuthor Commented:
Thank both you. The first does exactly what i need and more complete but i can get your script better. but i still need more work to make it work for me.
0
zequestionerAuthor Commented:
0
Steve KnightIT ConsultancyCommented:
BTW you can get the first part of a variable with %variable:~0,X% aswell as splitting up using for loops at the "." etc.

i.e. if you have IPADDRESS=192.168.1.99

then %IPADDRESS:~0,10% is 192.168.1.  - better make 10 digits so you can tell on 192.168.1. rather than 192.168.12.1 etc.

So you could use:

if "%ipaddress:~0,10%"=="192.168.1." (
  echo on subnet 192.168.1.x
)

etc.


SET /? from cmd prompt shows you more such options.

Steve
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Windows Batch

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.