如何查看IP网络阻塞?

How to check IP network block?

我在这里检查 IP 地址是否在给定范围内,即:在最低 IP 编号和最高 IP 编号之间。我尝试了一些循环遍历数组的找到的脚本,但它们给出了错误的响应。所以我使用下面的代码对其进行了分解,该代码工作了 2 天,直到它落入以下网络范围:

IP address = 40.77.167.163
Low = 40.74.0.0
High = 40.125.127.255

function validateIP(ip, low, high)

    validateIP = False
    Dim ipArray, lowArray, highArray, ipTotal

    ipArray = split(ip, ".")
    lowArray = split(low, ".")
    highArray = split(high, ".")

    if CInt(ipArray(3)) >= CInt(lowArray(3)) And CInt(ipArray(3)) <= CInt(highArray(3))then
        validate3 = "1"
    end if
    if CInt(ipArray(2)) >= CInt(lowArray(2)) And CInt(ipArray(2)) <= CInt(highArray(2))then
        validate2 = "1"
    end if
    if CInt(ipArray(1)) >= CInt(lowArray(1)) And CInt(ipArray(1)) <= CInt(highArray(1))then
        validate1 = "1"
    end if
    if CInt(ipArray(0)) >= CInt(lowArray(0)) And CInt(ipArray(0)) <= CInt(highArray(0))then
        validate0 = "1"
    end if

    ipTotal = validate0 & validate1 & validate2 & validate3
    if ipTotal = "1111" then
       validateIP = True
    end if

end function

出于某种我无法理解的原因,它不适用于这些值。但它适用于较小的块。

如果您正在验证 IPV4 地址,那么将 IP 地址转换为数字并检查给定 IP 是否落在您的低范围和高范围之间是非常直接的(不过将 IPV6 地址转换为数字有点复杂,特别是在 VBScript 中)

Function ipv4ToNumber(ByVal ipv4)

    Dim i, pos, PrevPos, num

    For i = 1 To 4

        pos = InStr(PrevPos + 1, ipv4, ".", 1)
        If i = 4 Then pos = Len(ipv4) + 1
        num = Int(Mid(ipv4, PrevPos + 1, pos - PrevPos - 1))
        PrevPos = pos
        ipv4ToNumber = ((num Mod 256) * (256 ^ (4 - i))) + ipv4ToNumber

    next

End function

Function validateIP(ByVal ip, ByVal Low, ByVal High)

    ip = ipv4ToNumber(ip)
    Low = ipv4ToNumber(Low)
    High = ipv4ToNumber(High)

    If ip >= Low AND ip <= High Then
        validateIP = True
    Else
        validateIP = False
    End If

End Function

validateIP("40.77.167.163","40.74.0.0","40.125.127.255")

输出:

True


编辑: 如果任何经典 ASP 开发人员想要将 IPV6 地址转换为数字,使用 VBScript 是不可能的(对我来说知识),但可以在 MySQL:

中完成
Function ipv6ToNumber(ByVal ipv6)

    Dim SQL : SQL = "SELECT CAST(" &_
        "CONV(" &_
            "SUBSTR(" &_
                "HEX(" &_
                    "INET6_ATON('" & ipv6 & "')" &_
                ")" &_
            ",1,16)" &_
        ",16,10" &_
    ")AS DECIMAL(65))" &_
    "*18446744073709551616" &_
    "+CAST(" &_
        "CONV(" &_
            "SUBSTR(" &_
                "HEX(" &_
                    "INET6_ATON('" & ipv6 & "')" &_
                ")" &_
            ",17,16)" &_
        ",16,10" &_
    ") AS DECIMAL(65))"

    'ipv6ToNumber = Execute using MySQL and return the number

End Function

或者使用服务器端javascript:

<script language="javascript" type="text/javascript" runat="server">

    function ipv6ToNumber(ip) {

        ip.replace("::",":0:0:0:")

        var parts = [];
        ip.split(":").forEach(function(it) {
            var bin = parseInt(it, 16).toString(2);
            while (bin.length < 16) {
                bin = "0" + bin;
            }
            parts.push(bin);
        })
        var bin = parts.join("");

        var dec = bigInt(bin, 2).toString();

        return dec;

    }

</script>