Jump to content

Checking Ip And Netmask


Recommended Posts

I want to check an IP address against a given network/netmask, but am not sure how to do this. For example:

ip=192.168.1.5 nm=192.168.1.0/24 = match

ip=192.168.1.5 nm=192.168.2.0/24 = no match

ip=192.168.1.5 nm=192.168.0.0/16 = match

ip=10.120.12.5 nm=192.168.2.0/24 = no match

ip=10.120.12.5 nm=10.120.12.5/32 = match

What is the best way to check for this?

Thanks,

-John

Link to comment
Share on other sites

I want to check an IP address against a given network/netmask, but am not sure how to do this. For example:

ip=192.168.1.5 nm=192.168.1.0/24 = match

ip=192.168.1.5 nm=192.168.2.0/24 = no match

ip=192.168.1.5 nm=192.168.0.0/16 = match

ip=10.120.12.5 nm=192.168.2.0/24 = no match

ip=10.120.12.5 nm=10.120.12.5/32 = match

What is the best way to check for this?

Thanks,

-John

shouldn't your 3rd one be a no match? (different subnet)

maybe something like

Dim $ips[2] = ['192.168.1.5','10.120.12.5']
Dim $nms[4] = ['192.168.1.0/24','192.168.2.0/24','192.168.0.0/16','10.120.12.5/32']

For $x = 0 To UBound($ips) - 1
    For $y = 0 To UBound($nms) - 1
        MsgBox(0,"Check Match", $ips[$x] & @LF & $nms[$y] & @LF & _CheckIPMatch($ips[$x],$nms[$y]))
    Next
Next

Func _CheckIPMatch($s_ip, $s_nm)
    Local $range, $i, $t_ip, $t_nm
    $t_ip = StringSplit($s_ip,".")
    $t_nm = StringSplit($s_nm,".")
    If $t_ip[1] = $t_nm[1] And $t_ip[2] = $t_nm[2] And $t_ip[3] = $t_nm[3] Then
            $range = StringSplit($t_nm[4],'/')
            If Int($t_ip[4]) >= Int($range[1]) And Int($t_ip[4]) <= Int($range[2]) Then
                Return "Match"
            EndIf
    Else
        Return "No Match"
    EndIf
EndFunc

SciTE for AutoItDirections for Submitting Standard UDFs

 

Don't argue with an idiot; people watching may not be able to tell the difference.

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...