Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

compare ip addr to a ip range

Status
Not open for further replies.

sknyppy

Programmer
May 14, 1999
137
0
0
US
Does anyone have a function that will check if an ip address falls with a starting/ending ip address range>>

Thanks,
Dave

 
convert the IP's into their decimal equivalent:

e.g. 10.20.30.40 -->

10 * 256 * 256 * 256 +
20 * 256 * 256 +
30 * 256 +
40

= 169,090,600

e.g.:

Code:
function IPtoNum (IPAddress)

  dim aIP: set aIP = split(IPAddress,".")
  if ubound(aIP) <> 3 then
     IPtoNum = 0
     Exit Function
  end if

  if isnumeric(aIP(0)) and isnumeric(aIP(1)) and isnumeric(aIP(2)) and isnumeric(aIP(3)) then
     IPToNum = (cint(aIP(0))*256*256*256) + (cint(aIP(1))*256*256) + (cint(aIP(2))*256) + (cint(aIP(3))
   else
      IPToNum = 0
   End if
end function

then you could use this as part of another function e.g

Code:
function IPinRange(StartIP, EndIP, TestIP)
iStart = IPtoNum(StartIP)
iEnd = IPtoNum(EndIP)
iTest = IPtoNum(TestIP)
if iStart=0 or iEnd=0 or iTest=0 then
  IPinRange = false
  exit function
end if
if iTest >= iStart and iTest <= iEnd then
  IPinRange = true
else
  IPinRange = false
end if

end function

watch out for typos etc, as that's freehand and not checked.

A smile is worth a thousand kind words. So smile, it's easy! :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top