APNIC是管理亚太地区IP地址分配的机构,它有着丰富准确的IP地址分配库,同时这些信息也是对外公开的!下面就让我们看看如何在Linux下获得一些电信运营商的IP地址分配情况:

shell> wget http://ftp.apnic.net/apnic/dbase/tools/ripe-dbase-client-v3.tar.gz

shell> tar xzvf ripe-dbase-client-v3.tar.gz

shell> cd whois-3.1

shell> ./configure

shell> make

完成上述编译安装工作后,我们开始获取IP地址段;

中国网通:

shell> ./whois3 -h whois.apnic.net -l -i mb MAINT-CNCGROUP > /var/cnc

中国电信:

shell> ./whois3 -h whois.apnic.net -l -i mb MAINT-CHINANET > /var/chinanet

中国铁通:

shell> ./whois3 -h whois.apnic.net -l -i mb MAINT-CN-CRTC > /var/crtc

打开获取后的文件可以看到里面的信息非常详细,甚至可以看到各个分公司的负责人、电话、电子邮件等等信息。如果想得到一份整齐干净的IP地址段文件,只要用grep和awk简单过滤就可以了。

以下脚本用whois判断每个地址段归属

#!/bin/sh
#auto get the IP Table
#get the newest delegated-apnic-latest
rm delegated-apnic-latest

if type wget
then wget http://ftp.apnic.net/apnic/stats/apnic/delegated-apnic-latest
else fetch http://ftp.apnic.net/apnic/stats/apnic/delegated-apnic-latest
fi
grep 'apnic|CN|ipv4' delegated-apnic-latest | cut -f 4,5 -d '|' | tr '|' ' ' | while read ip cnt
do
mask=$(bc <<END | tail -1
pow=32;
define log2(x) {
if (x<=1) return (pow);
pow--;
return(log2(x/2));
}
log2($cnt);
END
)
echo $ip/$mask';'>>cnnet

resultext=`whois $ip@whois.apnic.net | grep -e ^netname -e ^descr -e ^role -e ^mnt-by | cut -f 2 -d ':' | sed 's/ *//'`

if echo $resultext | grep -i -e 'railcom' -e 'crtc' -e 'railway'
then echo $ip/$mask';' >> crc
elif echo $resultext | grep -i -e 'cncgroup' -e 'netcom'
then echo $ip/$mask';' >> cnc
elif echo $resultext | grep -i -e 'chinanet' -e 'chinatel'
then echo $ip/$mask';' >> telcom_acl
elif echo $resultext | grep -i -e 'unicom'
then echo $ip/$mask';' >> unicom
elif echo $resultext | grep -i -e 'cmnet'
then echo $ip/$mask';' >> cmnet
else
echo $ip/$mask';' >> other_acl
fi

done

posted on 2011-08-23 22:30  长风大侠IV  阅读(767)  评论(0)    收藏  举报