如何检查IP网段?

时间:2019-05-01 07:58:55

标签: vbscript asp-classic

在这里,我正在检查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

出于某些原因,我无法使用这些值。但这适用于较小的块。

1 个答案:

答案 0 :(得分:2)

如果您要验证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”)

输出:

  


编辑::如果有任何经典的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>