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

公司最近也有自建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 -fapnic_ip_info
fi
if [ -e "apnic_all_ip" ];then
rm -fapnic_all_ip
fi
if [ -e save_dir/CNC_GROUP ];then
rm -fsave_dir/CNC_GROUP
fi
if [ -e save_dir/CHINATELECOM_GROUP ];then
rm -fsave_dir/CHINATELECOM_GROUP
fi
if [ -e save_dir/CERNET_GROUP ];then
rm -fsave_dir/CERNET_GROUP
fi
if [ -e save_dir/CRTC_GROUP ];then
rm -fsave_dir/CRTC_GROUP
fi
if [ -e save_dir/CHINAMOBILE_GROUP ];then
rm -fsave_dir/CHINAMOBILE_GROUP
fi
if [ -e save_dir/CHINAUNICOM_GROUP ];then
rm -fsave_dir/CHINAUNICOM_GROUP
fi
if [ -e save_dir/OTHERNET_GROUP ];then
rm -fsave_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 '{print2}' | xargs`
# CNC
into_cnc=`echo isp_info | sed -n '/CNC/p'`
# CHINATELECOM
into_chinatelecom=`echoisp_info | sed -n '/CHINANET/p'`
# CERNET
into_cernet=`echo isp_info | sed -n '/CERNET/p'`
# CRTC
into_crtc=`echoisp_info | sed -n '/CRTC/p'`
# CHINAMOBILE
into_cmcc=`echo isp_info | sed -n '/CMCC/p'`
# CHINAUNICOM
into_unicom=`echoisp_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

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注