文件名随你的心意,并创建脚本基础
#!/bin/bash
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
# this scrip write by Clang at 2014-08-12.
# discriminate per isp from apnic.
current_time=`date +"%Y-%m-%d %H:%M:%S"`
# define save ip result directory.
save_dir="你自己的缓存目录(用于缓存来自apnic的原始数据)"
# download ip info from apnic website.
apnic_ip_info="$save_dir/apnic_ip_info"#(缓存的APNIC文件)
# get all ip list values from apnic.
apnic_all_ipv4="$save_dir/apnic_all_ipv4"#全量IPV4数据
apnic_all_ipv6="$save_dir/apnic_all_ipv6"#全量IPV6数据
apnic_all_ipv4_hk="$save_dir/apnic_all_ipv4_hk"
apnic_all_ipv6_hk="$save_dir/apnic_all_ipv6_hk"
apnic_all_ipv4_mo="$save_dir/apnic_all_ipv4_mo"
apnic_all_ipv6_mo="$save_dir/apnic_all_ipv6_mo"
apnic_all_ipv4_tw="$save_dir/apnic_all_ipv4_tw"
apnic_all_ipv6_tw="$save_dir/apnic_all_ipv6_tw"
#以上6行见名知意,你也可以改任何的名字,只要你能认出来,后续会用到
if [ ! -d "$save_dir" ]; then
mkdir "$save_dir"
fi
Local_Dir="(这里是你网站的绝对目录,没有两边的括号)"
# delete old exist file.
rm -f $save_dir/*#清空前一天的目录
echo '<html xmlns="http://www.w3.org/1999/xhtml">' >>${save_dir}/chinatelecom.html
echo '<head profile="http://gmpg.org/xfn/11">' >>${save_dir}/chinatelecom.html
echo ' <link rel="icon" href="favicon.ico" type="image/x-icon" />' >>${save_dir}/chinatelecom.html
echo ' <link rel="shortcut icon" href="favicon.ico" type="image/x-icon" />' >>${save_dir}/chinatelecom.html
echo ' <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />' >>${save_dir}/chinatelecom.html
echo ' <meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7" />' >>${save_dir}/chinatelecom.html
echo ' <meta name="robots" content="all" />' >>${save_dir}/chinatelecom.html
echo ' <meta name="author" content="monchhi@monchhi.net,Clang" />' >>${save_dir}/chinatelecom.html
echo ' <meta name="Copyright" content="Monchhi27" />' >>${save_dir}/chinatelecom.html
echo ' <title>每日更新的电信IP段,联通IP段,铁通IP段,移动IP段及部分IDC云商IP段 - Monchhi27</title>' >>${save_dir}/chinatelecom.html
echo ' <STYLE>body {font-family:Verdana,Tahoma,"BitStream vera Sans",Arial,Helvetica,sans-serif;font-size:16px;}</STYLE>' >>${save_dir}/chinatelecom.html
echo '</head>' >>${save_dir}/chinatelecom.html
echo '<body>' >>${save_dir}/chinatelecom.html
echo "#########################################<br>" >>${save_dir}/chinatelecom.html
echo "# ISP 中国电信 所有IP地址段表<br>" >>${save_dir}/chinatelecom.html
echo "# 数据来自于: http://ispip.m27.online/<br>" >>${save_dir}/chinatelecom.html
echo "# 数据生成时间:"${current_time}"<br>" >>${save_dir}/chinatelecom.html
echo "# 脚本自动生成,如有错漏或任何建议,请联系monchhi[at]monchhi.net<br>" >>${save_dir}/chinatelecom.html
echo "################IPV4网段#################<br>" >>${save_dir}/chinatelecom.html
wget -c --progress=bar:force --no-check-certificate http://ftp.apnic.net/apnic/stats/apnic/delegated-apnic-latest -O $apnic_ip_info
#从APNIC获取全量数据
grep "apnic|CN|ipv4|" "$apnic_ip_info" | awk -F'|' '{print $4"/"32-log($5)/log(2)}' > "$apnic_all_ipv4"
#获取中国全量IPV4数据,涉及了正则表达式,并存入指定文件,与第一部分有关
while read line
do
isp_ip=`echo $line | awk -F'/' '{print $1}'`
isp_info=`whois -h whois.apnic.net $isp_ip | grep -E "(descr|mnt-|netname|e-mail)" `
isp_info1=`echo $isp_info | awk '{print $0}' | xargs`
tmp=${isp_info#*descr:} # Remove everything up to and including first -
tmp2=${tmp%%mnt-by:*} # Remove the first - and everything following it
tmp3=${tmp2%%remarks:*} # Remove the first - and everything following it
isp_info2_1=${tmp3%%descr:*} # Remove the first - and everything following it
isp_info2=`echo $isp_info2_1 | xargs -0`
tmp4=${isp_info#*netname:} # Remove everything up to and including first -
isp_info3_1=${tmp4%%descr:*} # Remove the first - and everything following it
isp_info3=`echo $isp_info3_1 | xargs -0`
#上述可以简化,实在是懒得修改,本文直接粘贴正在使用的脚本了。因为本人有相应的需求,故而拆分脚本步骤,并做更细致的匹配。详情请见实际项目中 广电、其他 两个分类
# CHINATELECOM
into_chinatelecom=`echo $isp_info1 | sed -n '/CHINANET/p'`
# into_telecom=`echo $isp_info1 | sed -n '/TELECOM/p'`
into_bjtel=`echo $isp_info1 | sed -n '/BJTEL/p'`
into_chinatelecom1=`echo $isp_info1 | sed -n '/chinatelecom.cn/p'`
#上述为使用关键字进行匹配,逻辑关系是,通过whois查询相应IP的信息,并截取,截取后对内容进行匹配,而后存入临时变量。请注意:中间有一行注释,之所以保留,是因为在实际使用过程中发现会出现匹配异常,但本人才疏学浅,不会处理,故而在脚本不断迭代更新时,做了保留。
if [ "${into_chinatelecom}" != "" ];then
echo "$line<br>" >> ${save_dir}/chinatelecom.html
elif [ "${into_chinatelecom1}" != "" ];then
echo "$line<br>" >> ${save_dir}/chinatelecom.html
# elif [ "${into_telecom}" != "" ];then
# echo "$line<br>" >> ${save_dir}/chinatelecom.html
elif [ "${into_bjtel}" != "" ];then
echo "$line<br>" >> ${save_dir}/chinatelecom.html
fi
done<$apnic_all_ipv4
echo "################### END ###################<br><br>" >>${save_dir}/chinatelecom.html
echo '©Monchhi.Net | <a href="htp://www.yangmengqi.cn/">Monchhi27</a>' >>${save_dir}/chinatelecom.html
echo '</body>' >>${save_dir}/chinatelecom.html
echo '</html>' >>${save_dir}/chinatelecom.html
sed -i "s/strTime<br>$/$(date +"%Y-%m-%d %H:%M:%S")<br>/g" ${save_dir}/chinatelecom.html
rm -rf ${Local_Dir}/Old/$(date -d -30day +"%Y%m%d")
#重命名前一天的IP文件
if [ ! -d ${Local_Dir}/Old/$(date -d -1day +"%Y%m%d") ]; then
mkdir ${Local_Dir}/Old/$(date -d -1day +"%Y%m%d")
fi
mv -f ${Local_Dir}/chinatelecom.html ${Local_Dir}/Old/$(date -d -1day +"%Y%m%d")/chinatelecom.html
cp -f ${Local_Dir}/index_old.php ${Local_Dir}/Old/$(date -d -1day +"%Y%m%d")/index.php
#复制当天的IP文件
cp -f ${save_dir}/*.html ${Local_Dir}/
#更改权限
chown -R www:www ${Local_Dir}/*
上述内容请自行观看,懒得码字了,抱一丝抱一丝
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。