Link to home
Start Free TrialLog in
Avatar of ronayers
ronayers

asked on

How do I determine if IP is in IP Range?

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
Avatar of Stephen Manderson
Stephen Manderson
Flag of United Kingdom of Great Britain and Northern Ireland image

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

Need to change the 'Or' in line 7 to 'And'
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
Avatar of ronayers
ronayers

ASKER

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
ASKER CERTIFIED SOLUTION
Avatar of troyw1
troyw1
Flag of United States of America image

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