shell列出所有的运营商的ip地址范围

2012年12月20日 发表评论 阅读评论

公司最近也有自建CDN的打算,无意在网上看到一则通过下apnic的数据并进行shell脚本区分的方法。感觉挺方便的,虽然在台机上测试效率有点低,不过服务器上效果应该还不错。可以自动区分电信_网通_教育网_铁通_移动_联通ip地址范围,用于做智能DNS解析应该不错。脚本如下:

#!/bin/sh
# download ip info from apnic website.
apnic_ip_info="/home/www/apnic_ip_info"
# get all ip list values from apnic.
apnic_all_ip="/home/www/apnic_all_ip"
# define save ip result directory.
save_dir="/home/www"
# delete old exist file.
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 http://ftp.apnic.net/apnic/stats/apnic/delegated-apnic-latest -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`
# CNC
into_cnc=`echo $isp_info | sed -n '/CNC/p'`
# CHINATELECOM
into_chinatelecom=`echo $isp_info | sed -n '/CHINANET/p'`
# CERNET
into_cernet=`echo $isp_info | sed -n '/CERNET/p'`
# CRTC
into_crtc=`echo $isp_info | sed -n '/CRTC/p'`
# CHINAMOBILE
into_cmcc=`echo $isp_info | sed -n '/CMCC/p'`
# CHINAUNICOM
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




本站的发展离不开您的资助,金额随意,欢迎来赏!

You can donate through PayPal.
My paypal id: itybku@139.com
Paypal page: https://www.paypal.me/361way

分类: shell脚本学习 标签: ,
  1. 本文目前尚无任何评论.