网络编程
位置:首页>> 网络编程>> Asp编程>> asp判断ip及ip段范围的一组函数小记

asp判断ip及ip段范围的一组函数小记

作者:hayden 来源:烦恼's BLOG 发布时间:2008-12-09 18:23:00 

标签:ip,函数,判断,asp

最近在工作中涉及到判断服务器所在ip反馈程序使用情况的程序

主要要求就是,本机或局域网调试程序时,不反馈其域名(localhost)或ip

站长工具推荐:IP/域名归属地查询

在网上查了一下资料,比较零散,在此整理一下:

下面的asp函数判断是否为ip

'返回true 或 false
function chk_ip(strIP) 
    dim boolIsIP     
    dim arrIP 
    boolIsIP=true   '函数初始值为true 
    arrIP=split(strIP,".")   '将输入的IP用"."分割为数组,数组下标从0开始,所以有效IP分割后的数组上界必须为3 
    if ubound(arrIP)<>3 then 
        boolIsIP=false   
    else 
        for intLoop=0 to Ubound(arrIP) 
            if not isnumeric(arrIP(intLoop)) then   '检查数组元素中各项是否为数字,如果不是则不是有效IP 
                boolIsIP=false 
            else 
                if arrIP(intLoop)>255 or arrIP(intLoop)<0 then '检查IP数字是否满足IP的取值范围 
                boolIsIP=false 
                end if 
            end if 
        next 
    end if 
    chk_ip=boolIsIp 
end Function

下面实现检测目标ip是否在某ip段中

其中UserIP是我们要检测的IP

NetIP是要检测的网段或某个IP,用xxx.xxx.xxx.xxx/N来表示网段,其中N表示子网掩码位数

'作者:环球万维
Function check_ip(UserIp,NetIP)
    currentip=UserIp
    collection_ips=split(iplist,",") '将网络按点分割成4段
    check_ip=false '初始函数值,false假设IP不在这网段
    NetIP=trim(NetIP)
    slashPos=inStr(NetIP,"/")
    if slashPos=0 then '网段没含有/符号,他只是一个IP,所以比较比个字符串是否相同就可以了
        if NetIP=currentip then
            check_ip=true 'check_ip=true表示IP相等
            exit function
        end if
    else
        netRang=mid(NetIP,slashPos+1) '得到/后边的数字
        if not isNumeric(netRang) Then exit function '/后边不是数字,格式不正确
        netRang=cint(netRang) '将字符转为数字
        if netRang>31 Then exit function '/后的数字不能超过32位
        ipsets=split(currentip,".") '将用户IP按点分成四段
C_IP_BIN=pad(dec2bin(ipsets(0))) & pad(dec2bin(ipsets(1))) & pad(dec2bin(ipsets(2))) & pad(dec2bin(ipsets(3)))
        '上边这行是将用户IP地址手工转换为对应的一个32个字符长的二进制
        ipsets=split(NetIP,".") '按上边的过程将网段IP同样转为32个字符长的二进制
        sPos=instr(ipsets(3),"/") '最后一点格式应该是 数字/数字
        if sPos=0 Then exit function
        ipsets(3)=left(ipsets(3),sPos-1) '得到最后一段/前边的数字
        S_IP_BIN=pad(dec2bin(ipsets(0))) & pad(dec2bin(ipsets(1))) & pad(dec2bin(ipsets(2))) & pad(dec2bin(ipsets(3)))
        '将其转换为32个字符长的二进制
        if left(C_IP_BIN,netRang) = left(S_IP_BIN,netRang) Then check_ip=true '比较网段络是否相同就可以判断用户IP否属于某个网段了
    end if
end Function
'将十进制转为二进制字符串
function dec2bin(octNumber)
    vara=octNumber
    do 
        dec2bin=cstr(vara mod 2) & dec2bin
        vara=vara \ 2
    loop until vara=0
end function
'将二进制字符串填充为8位
function pad(str)
    pad=right("00000000" & str,8)
end function

其中这个判断ip段的函数,在调用时是:

check_ip("192.168.1.1","192.168.0.0/16") 

然后我就突然发现,那后面的/16是什么意思?

这些组网等知识,虽说在实际过程中比较行,但说起理论来还是差一点

继续,百度一下:在此记录唉~

引用内容

192.168.1.0/16 它的子网掩码 255.255.0.0(这是十进制)
11111111.11111111.00000000.00000000(这是二进制)
可见,前面有16个1
那么 127.0.0.0/8 可想而知啦:前面是8个1 子网掩码:255.0.0.0
二进制:11111111.00000000.00000000.00000000
所包括的网段为:127.0.0.0-127.255.255.255

哈哈,还有其它的常用局域网IP 也在此记记:

引用内容

10.0.0.0/8:10.0.0.0~10.255.255.255 
172.16.0.0/12:172.16.0.0~172.31.255.255
192.168.0.0/16:192.168.0.0~192.168.255.255
0
投稿

猜你喜欢

手机版 网络编程 asp之家 www.aspxhome.com