Jump to content
Sign in to follow this  
jftuga

Checking Ip And Netmask

Recommended Posts

jftuga

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

Share this post


Link to post
Share on other sites
GaryFrost

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.

 

Share this post


Link to post
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
Sign in to follow this  

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.