[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1659
  • Last Modified:

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?
0
zequestioner
Asked:
zequestioner
  • 3
  • 2
2 Solutions
 
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now