SHELL从APNIC获取IP地址

如下代码:菜鸟写的


#!/bin/bash
wget http://ftp.apnic.net/apnic/stats/apnic/delegated-apnic-latest
FILE=delegated-apnic-latest
grep "apnic|CN|ipv4" $FILE | awk -F "|" '{print $4,$5}' > IP.txt
AB=`cat IP.txt | wc -l`
pow=32
i=1
while(($i<=$AB));do
        IP=`sed -n ${i}p IP.txt | awk '{print $1}'`
        HOST=`sed -n ${i}p IP.txt | awk '{print $2}'`
        ERHOST=`echo "obase=2;$HOST" | bc`
        HOSTL=`echo ${#ERHOST}`
        let BU=$pow-$HOSTL
        ZQIAN="11111111111111111111111111111111"
        QIAN=`echo ${ZQIAN:0:$BU}`
        MASK=`echo $QIAN$ERHOST`
        STRA=`echo ${MASK:0:8}`
        STRB=`echo ${MASK:8:8}`
        STRC=`echo ${MASK:16:8}`
        STRD=`echo ${MASK:24:8}`
        MASKA=`echo "ibase=2;$STRA" | bc`
        MASKB=`echo "ibase=2;$STRB" | bc`
        MASKC=`echo "ibase=2;$STRC" | bc`
        MASKD=`echo "ibase=2;$STRD" | bc`
        NETMASK=`echo $MASKA.$MASKB.$MASKC.$MASKD`
        BIT=`echo $MASK | grep -o '1' | wc -l`
        echo $IP/$NETMASK
        echo $IP/$NETMASK >> IP.SH
        let i++
done
rm -rf IP.txt $FILE

获得的IP格式如下:

223.112.0.0/255.252.0.0
223.116.0.0/255.254.0.0
223.120.0.0/255.248.0.0
223.128.0.0/255.254.0.0
223.144.0.0/255.240.0.0
223.160.0.0/255.252.0.0


你可能感兴趣的:(shell,IP,bash)