#!/bin/bash
FILE=ip_apnic 
rm -rf $FILE isp province cn.net 
mkdir isp province 
wget http://ftp.apnic.net/apnic/stats/apnic/delegated-apnic-latest -O $FILE 
grep 'apnic|CN|ipv4|' $FILE | cut -f 4,5 -d'|'|sed -e 's/|/ /g' | while read ip cnt 
do 
    mask=$(cat <<- EOF | bc | tail -1 
        pow=32;
        define log2(x) 
        { 
            if (x<=1) return (pow); 
            pow--; 
            return(log2(x/2)); 
        }    
        log2($cnt) 
        EOF
    ) 
        echo $ip/$mask 
        echo $ip/$mask >> cn.net
        NETNAME_ALL=(`whois -h whois.apnic.net  $ip | grep ^netname |  sed -e 's/.*:        \(.*\)/\1/g'|awk  -F- '{print $1,$2}'`)
    #count ISP
    case ${NETNAME_ALL[0]} in 
        CNC) 
                echo $ip/$mask >> isp/CNC 
        ;; 
        CNCGROUP) 
                echo $ip/$mask >> isp/CNC 
        ;;
        UNI)
                echo $ip/$mask >> isp/UNICOM
        ;;
        UNICOM)
                echo $ip/$mask >> isp/UNICOM
        ;; 
        CHINANET) 
                echo $ip/$mask >> isp/CHINANET 
        ;; 
        CHINATELE) 
                echo $ip/$mask >> isp/CHINANET
        ;; 
        CHINATELCOM) 
                echo $ip/$mask >> isp/CHINANET
        ;;
        CHINAENTERCOM)
                echo $ip/$mask >> isp/CHINANET
        ;;
        CMNET)
                echo $ip/$mask >> isp/CMNET
        ;;
        CHINAFIC)
                echo $ip/$mask >> isp/CMNET
        ;;
        CRTC) 
                echo $ip/$mask >> isp/CRTC 
        ;;
        RAILCOM) 
                echo $ip/$mask >> isp/CRTC 
        ;; 
        *) 
                echo $ip/$mask >> isp/OTHER
        ;; 
        esac 
    #count province
    case ${NETNAME_ALL[1]} in
    FJ*|fj*|FuZhou|fuzhou)
        echo "$ip/$mask" >> province/fujian
    ;;
    GD*)
        echo "$ip/$mask" >> province/guangdong
    ;;
    NM)
        echo "$ip/$mask" >> province/neimenggu
    ;;
    GZ)
        echo "$ip/$mask" >> province/guizhou
    ;;
    NX|NINGXIA)
        echo "$ip/$mask" >> province/ningxia
    ;;
    HL*)
        echo "$ip/$mask" >> province/heilongjiang
    ;;
    SX|TY)
        echo "$ip/$mask" >> province/shanxi
    ;;
    SN|SHAANXI)
        echo "$ip/$mask" >> province/shannxi
    ;;
    HA)
        echo "$ip/$mask" >> province/henan
    ;;
    BJ)
        echo "$ip/$mask" >> province/beijing
    ;;
    CQ)
        echo "$ip/$mask" >> province/chongqing
    ;;
    KM|YN)
        echo "$ip/$mask" >> province/yunan
    ;;
    HB|DIAQOS1)
        echo "$ip/$mask" >> province/hubei
    ;;
    XZ)
        echo "$ip/$mask" >> province/xizang
    ;;
    HE)
        echo "$ip/$mask" >> province/hebei
    ;;
    SD)
        echo "$ip/$mask" >> province/shandong
    ;;
    GS)
        echo "$ip/$mask" >> province/gansu
    ;;
    AH|Anhui)
        echo "$ip/$mask" >> province/anhui
    ;;
    LN)
        echo "$ip/$mask" >> province/liaoning
    ;;
    HN|HUNAN)
        echo "$ip/$mask" >> province/hunan
    ;;
    JS|SZ)
        echo "$ip/$mask" >> province/jiangsu
    ;;
    XJ)
        echo "$ip/$mask" >> province/xinjiang
    ;;
    JX)
        echo "$ip/$mask" >> province/jiangxi
    ;;
    JL)
        echo "$ip/$mask" >> province/jilin
    ;;
    SH|INSURANCE)
        echo "$ip/$mask" >> province/shanghai
    ;;
    GX)
        echo "$ip/$mask" >> province/guangxi
    ;;
    HI)
        echo "$ip/$mask" >> province/hainan
    ;;
    TJ)
        echo "$ip/$mask" >> province/tianjin
    ;;
    SC)
        echo "$ip/$mask" >> province/sichuan
    ;;
    QH|GEERMU)
        echo "$ip/$mask" >> province/qinghai
    ;;
    HK)
        echo "$ip/$mask" >> province/xianggang
    ;;
    ZJ)
        echo "$ip/$mask" >> province/zhejiang
    ;;
    *)
        echo "$ip/$mask" >> province/_other
    ;;  
    esac
done