Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2543082
  • 博文数量: 271
  • 博客积分: 6659
  • 博客等级: 准将
  • 技术积分: 3141
  • 用 户 组: 普通用户
  • 注册时间: 2009-11-17 10:24
文章分类

全部博文(271)

文章存档

2016年(2)

2015年(12)

2014年(7)

2013年(19)

2012年(22)

2011年(81)

2010年(128)

分类: LINUX

2010-04-30 01:47:36

APNIC是管理亚太地区IP地址分配的机构,它有着丰富准确的IP地址分配库,同时这些信息也是对外公开的!下面就让我们看看如何在 Linux下获得一些电信运营商的IP地址分配情况:有时搞一些跨网段的工程和应用,需要尽量准确的知道电信、网通、铁通等电信运营商的IP地址段分配情况.
 
 
找到最新的whois版本,如:ripe-dbase-client-v3.tar.gz下载
第一步:
zj@zj:~$ wget
--21:49:31-- 
           => `ripe-dbase-client-v3.tar.gz'
Resolving ftp.apnic.net... 202.12.29.20
Connecting to ftp.apnic.net|202.12.29.20|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 26,342 (26K) [application/x-gzip]

100%[====================================>] 26,342        --.--K/s             

21:49:32 (6.24 MB/s) - `ripe-dbase-client-v3.tar.gz' saved [26342/26342]

zj@zj:~$ ls | grep ripe-dbase-client-v3.tar.gzripe-dbase-client-v3.tar.gz
zj@zj:~$ tar xvf ripe-dbase-client-v3.tar.gz
whois-3.1/
whois-3.1/ChangeLog
whois-3.1/FLAGS.txt
whois-3.1/HELP.txt
whois-3.1/README
whois-3.1/aclocal.m4
whois-3.1/flags2hdr.pl
whois-3.1/help2nroff.pl
whois-3.1/whois3.c
whois-3.1/whois3.1.i
whois-3.1/whois3.1.iii
whois-3.1/Makefile.in
whois-3.1/configure.in
whois-3.1/configure
whois-3.1/mkinstalldirs
whois-3.1/install-sh
zj@zj:~$ cd whois-3.1/
zj@zj:~/whois-3.1$ ls
aclocal.m4  configure.in  help2nroff.pl  Makefile.in    whois3.1.i
ChangeLog   flags2hdr.pl  HELP.txt       mkinstalldirs  whois3.1.iii
configure   FLAGS.txt     install-sh     README         whois3.c
zj@zj:~/whois-3.1$ ./configure
creating cache ./config.cache
checking for gcc... gcc
checking whether the C compiler (gcc  ) works... yes
checking whether the C compiler (gcc  ) is a cross-compiler... no
checking whether we are using GNU C... yes
checking whether gcc accepts -g... yes
checking for a BSD compatible install... /usr/bin/install -c
checking whether ln -s works... yes
checking how to run the C preprocessor... gcc -E
checking for ANSI C header files... yes
checking for limits.h... yes
checking for unistd.h... yes
checking for working const... yes
checking return type of signal handlers... void
checking for gethostbyname... yes
checking for socket... yes
checking for strerror... yes
checking for strtol... yes
updating cache ./config.cache
creating ./config.status
creating Makefile
zj@zj:~/whois-3.1$ make
perl flags2hdr.pl flags.h < FLAGS.txt > flags.h
gcc -I. -g -O2 -DPRODUCT=\"whois3\" -DVERSION=\"3.1\" -DSTDC_HEADERS=1 -DHAVE_LIMITS_H=1 -DHAVE_UNISTD_H=1 -DRETSIGTYPE=void -DHAVE_STRERROR=1 -DHAVE_STRTOL=1    -c -o whois3.o whois3.c
gcc -I. -g -O2 -DPRODUCT=\"whois3\" -DVERSION=\"3.1\" -DSTDC_HEADERS=1 -DHAVE_LIMITS_H=1 -DHAVE_UNISTD_H=1 -DRETSIGTYPE=void -DHAVE_STRERROR=1 -DHAVE_STRTOL=1  -o whois3 whois3.o
perl help2nroff.pl < HELP.txt > whois3.1.ii
cat whois3.1.i whois3.1.ii whois3.1.iii > whois3.1

完成上述编译安装工作后,我们开始获取IP地址段;
中国网通:zj@zj:~/whois-3.1$ ./whois3 -h whois.apnic.net -l -i mb MAINT-CNCGROUP > cnc
中国电信:zj@zj:~/whois-3.1$ ./whois3 -h whois.apnic.net -l -i mb MAINT-CHINANET > chinanet
中国铁通:zj@zj:~/whois-3.1$ ./whois3 -h whois.apnic.net -l -i mb MAINT-CN-CRTC > crtc
 
以下相应的帮助:
 
 ./whois3 --help



过滤整理
cat chinanet | col -b >chinanet.txt
用col -b 过滤掉控制字符
 
 
如果你是个难人的话,以下的脚本可以帮帮你,自动生成电信,网通,其他的脚本.

贴入以下的内容:(是一个相当优秀的脚本)
 
zj@zj:~/whois-3.1$ cat apnic.sh
#!/bin/bash
FILE=./ip_apnic
rm -f $FILE
wget -O $FILE
grep 'apnic|CN|ipv4|' $FILE | cut -f 4,5 -d'|'|sed -e 's/|/ /g' | while read ip cnt
do
        echo $ip:$cnt
        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>> cn.net
        NETNAME=`whois $ip@whois.apnic.net | sed -e '/./{H;$!d;}' -e 'x;/netnum/!d' |grep ^netname | sed -e 's/.*: \(.*\)/\1/g' | sed -e 's/-.*//g'`
        NETNAME=`echo $NETNAME | sed -e 's/cJ/ /g' | awk -F' ' '{ printf $1; }'`
       case $NETNAME in
       CNC)
               echo $ip/$mask >> CNCGROUP
       ;;
       CHINANET|CNCGROUP)
               echo $ip/$mask >> $NETNAME
       ;;
       CHINATELECOM)
               echo $ip/$mask >> CHINANET
       ;;
       *)
               echo $ip/$mask >> OTHER
       ;;
       esac
done

 
chmod +x apnic.sh
./apnic.sh
在当目前目录就会成以下三个文件
CHINANET CNCGROUP OTHER

这个脚本太强大了.膜拜下.你用了就知道了....
阅读(1474) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~