因为都是在线生成和查询,所以要耗费时间,我已经生成电信,网通等的ip段.红色字体为程序或者命令.
一.PHP脚本,本人兴趣所写,如有不足,请更正修改,使用方法,保存为ip.php,因为在shell/命令行下使用的,运行方法为:php ip.php :
$ipsplit=split("[\n]+",$iplist);
foreach($ipsplit as $val){
if(preg_match("/apnic\|CN\|ipv4\|/",$val)){
list($nic,$CN,$ver,$ip,$mask)=preg_split("/\|/",$val);
$iparray['set'][]=array("ip"=>$ip,"mask"=>$mask);
}
}
foreach($iparray['set'] as $val){
echo $val['ip']."\t".(32-log($val['mask'],2))."\n";
$flag=GetWhois($val['ip']);
switch($flag){
case "CHINANET":
$fpc=fopen("CHINANET","a+");
fwrite($fpc,$val['ip']."/".(32-log($val['mask'],2))."\n");
break;
case "CNC":
$fpc=fopen("CNC","a+");
fwrite($fpc,$val['ip']."/".(32-log($val['mask'],2))."\n");
break;
break;
case "CRTC":
$fpc=fopen("CRTC","a+");
fwrite($fpc,$val['ip']."/".(32-log($val['mask'],2))."\n");
break;
default:
$fpc=fopen("OTHERS","a+");
fwrite($fpc,$val['ip']."/".(32-log($val['mask'],2))."\n");
break;
}
}
function GetWhois($IP){
$rootwhois = 'whois.apnic.net';
$buffer1 = ReadSocket($rootwhois,$IP);
$flag="";
$buffer_result=nl2br($buffer1);
if(preg_match("/CHINANET/",$buffer_result)){
$flag="CHINANET";
return $flag;
}
if(preg_match("/CNC/",$buffer_result)){
$flag="CNC";
return $flag;
}
if(preg_match("/CRTC/",$buffer_result)){
$flag="CRTC";
return $flag;
}
if(!preg_match("/CHINANET|CRTC|CNC/",$buffer_result)){
$flag="OTHERS";
return $flag;
}
}
function ReadSocket($whois,$ip)
{
$buffer = '';
if (!$sock = fsockopen( $whois, 43, $errNum, $errStr, 20))
{
echo "Sorry,Can't fsockopen it";
}
else
{
fputs($sock,"$ip\n");
//$buffer = fread($sock, 8192);
while(!feof($sock)) $buffer.=fgets($sock, 8192);
fclose($sock);
}
return $buffer;
}
?>
方法二.在Linux/Unix/BSD下,保存以下内容,比如保存为ip_apinc.sh,改其权限为可以执行,然后运行。
#!/bin/sh
FILE=/home/ip_apnic
rm -f $FILE
rm -f CNC
rm -f OTHER
rm -f CHINANET
rm -f CRTC
wget -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=`whois $ip | grep ^netname | sed -e 's/.*: \(.*\)/\1/g' | sed -e 's/-.*//g'`
echo $NETNAME;
case $NETNAME in
CNC)
echo $ip/$mask >> CNC
;;
CNCGROUP)
echo $ip/$mask >> CNC
;;
CHINANET)
echo $ip/$mask >> CHINANET
;;
CHINATELECOM)
echo $ip/$mask >> CHINANET
;;
CRTC)
echo $ip/$mask >> CRTC
;;
*)
echo $ip/$mask >> OTHER
;;
esac
done
方法三,这个也是在Linux/Unix/BSD下的:
APNIC是管理亚太地区IP地址分配的机构,它有着丰富准确的IP地址分配库,同时这些信息也是对外公开的!下面就让我们看看如何在Linux下获得一些电信运营商的IP地址分配情况:
$
$ tar xzvf ripe-dbase-client-v3.tar.gz
$ cd whois-3.1
$ ./configure
$ make;make install完成上述编译安装工作后,我们开始获取IP地址段;
中国网通:-bash-2.05b$ whois3 -h whois.apnic.net -l -i mb MAINT-CNCGROUP > /var/cnc
中国电信:-bash-2.05b$ whois3 -h whois.apnic.net -l -i mb MAINT-CHINANET > /var/chinanet
中国铁通:-bash-2.05b$ whois3 -h whois.apnic.net -l -i mb MAINT-CN-CRTC > /var/crtc
打开获取后的文件可以看到里面的信息非常详细,甚至可以看到各个分公司的负责人、电话、电子邮件等等信息。如果想得到一份整齐干净的IP地址段文件,只要用grep和awk简单过滤就可以了:)
方法四,在win下的,利用电信路由生成器.exe/网通路由生成器.exe,这两个工具来生成.
#!/bin/sh
apnic_ip_info="apnic_ip_info"
apnic_all_ip="apnic_all_ip"
save_dir="./"
if [ -e "$apnic_ip_info" ];then
rm -f $apnic_ip_info
fi
if [ -e "$apnic_all_ip" ];then
rm -f $apnic_all_ip
fi
if [ -e $save_dir/CNC_GROUP ];then
rm -f $save_dir/CNC_GROUP
fi
if [ -e $save_dir/CHINATELECOM_GROUP ];then
rm -f $save_dir/CHINATELECOM_GROUP
fi
if [ -e $save_dir/CERNET_GROUP ];then
rm -f $save_dir/CERNET_GROUP
fi
if [ -e $save_dir/CRTC_GROUP ];then
rm -f $save_dir/CRTC_GROUP
fi
if [ -e $save_dir/CHINAMOBILE_GROUP ];then
rm -f $save_dir/CHINAMOBILE_GROUP
fi
if [ -e $save_dir/CHINAUNICOM_GROUP ];then
rm -f $save_dir/CHINAUNICOM_GROUP
fi
if [ -e $save_dir/OTHERNET_GROUP ];then
rm -f $save_dir/OTHERNET_GROUP
fi
wget -q -O $apnic_ip_info
grep "apnic|CN|ipv4|" "$apnic_ip_info" | awk -F'|' '{print $4}' > "$apnic_all_ip"
while read line
do
isp_info=`whois $line | grep -E "(mnt-|netname)" | awk '{print $2}' | xargs`
into_cnc=`echo $isp_info | sed -n '/CNC/p'`
into_chinatelecom=`echo $isp_info | sed -n '/CHINANET/p'`
into_cernet=`echo $isp_info | sed -n '/CERNET/p'`
into_crtc=`echo $isp_info | sed -n '/CRTC/p'`
into_cmcc=`echo $isp_info | sed -n '/CMCC/p'`
into_unicom=`echo $isp_info | sed -n '/UNICOM/p'`
if [ "${into_cnc}" != "" ];then
echo "$line CNC_GROUP" >> ${save_dir}/CNC_GROUP
elif [ "${into_chinatelecom}" != "" ];then
echo "$line CHINATELECOM_GROUP" >> ${save_dir}/CHINATELECOM_GROUP
elif [ "${into_cernet}" != "" ];then
echo "$line CERNET_GROUP" >> ${save_dir}/CERNET_GROUP
elif [ "${into_crtc}" != "" ];then
echo "$line CRTC_GROUP" >> ${save_dir}/CRTC_GROUP
elif [ "${into_cmcc}" != "" ];then
echo "$line CHINAMOBLIE_GROUP" >> ${save_dir}/CHINAMOBILE_GROUP
elif [ "${into_unicom}" != "" ];then
echo "$line CHINAUNICOM_GROUP" >> ${save_dir}/CHINAUNICOM_GROUP
else
echo "$line OTHERNET_GROUP" >> ${save_dir}/OTHERNET_GROUP
fi
done<$apnic_all_ip