Solved

How do I determine if IP is in IP Range?

Posted on 2008-10-07
7
1,368 Views
Last Modified: 2012-06-27
I want to check to see if an IP is in  a supplied range of IP's. For example, is the IP 192.168.1.50 in the range of 192.168.1.25 - 192.168.1.68. How could i do that with VB.net?

Thanks
0
Comment
Question by:ronayers
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
7 Comments
 
LVL 19

Expert Comment

by:Stephen Manderson
ID: 22662017
Hi There here is one way of doing it..
                   ' Start                   'Finish               'Check For
IsBetween("192.168.1.25 ", "192.168.1.68", "192.168.1.78") Returns False
IsBetween("192.168.1.25 ", "192.168.1.68", "192.168.1.50") Returns True

Regards
Steve

    Public Function IsBetween(ByVal Start As String, ByVal Finish As String, ByVal CheckFor As String) As Boolean
 
        Dim StartRange As Long = CLng(Start.Replace(".", ""))
        Dim FinishRange As Long = CLng(Finish.Replace(".", ""))
        Dim CheckForIP As Long = CLng(CheckFor.Replace(".", ""))
 
        If StartRange >= CheckForIP Or CheckForIP <= FinishRange Then
            Return True
        Else
            Return False
        End If
 
    End Function

Open in new window

0
 
LVL 19

Expert Comment

by:Stephen Manderson
ID: 22662031
Need to change the 'Or' in line 7 to 'And'
0
 
LVL 3

Expert Comment

by:troyw1
ID: 22670384
The posted code will not work properly because 192.168.12.6 would be consider in-range when it is not. Try this code instead:

http://www.freevbcode.com/ShowCode.asp?ID=7288
0
 

Author Comment

by:ronayers
ID: 22680723
troyw1:

that will just tell me the subnet the IPs are own based off a range. That will not solve my problem. Any other ideas?

thanks
0
 
LVL 3

Accepted Solution

by:
troyw1 earned 500 total points
ID: 22681855
I tried to find the example code I have used before, but I can't find it and I no longer access to the customer code that I wrote. It was a function that converted IP addresses to their correct integer value.

Here is another interesting approach that should work also; comparing each octet range, one at a time.
'Set the values to test
RangeMin = "192.168.10.1"
RangeMax = "192.168.10.50"
TestIP = "192.168.10.55"
 
 
'Split the above values into arrays
MinAry = Split(RangeMin, ".", -1, 1)
MaxAry = Split(RangeMax, ".", -1, 1)
IPAry = Split(TestIP, ".", -1, 1)
 
 
'Test the octets
error = ""
If IPAry(0) < MinAry(0) OR IPAry(0) > MaxAry(0) Then
error = "First octect out of range"
ElseIF IPAry(1) < MinAry(1) OR IPAry(1) > MaxAry(1) Then
error = "Second octect out of range"
ElseIF IPAry(2) < MinAry(2) OR IPAry(2) > MaxAry(2) Then
error = "Third octect out of range"
ElseIF IPAry(3) < MinAry(3) OR IPAry(3) > MaxAry(3) Then
error = "Fourth octect out of range"
End If
 
'Display the results
If error<>"" Then
MsgBox error, 16, "Processing Complete"
Else
MsgBox "The IP is within range", 1, "Processing Complete"
End If

Open in new window

0

Featured Post

Get HTML5 Certified

Want to be a web developer? You'll need to know HTML. Prepare for HTML5 certification by enrolling in July's Course of the Month! It's free for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question