前往小程序,Get更优阅读体验!
立即前往
发布
社区首页 >专栏 >获取国内运营商IP数据

获取国内运营商IP数据

原创
作者头像
是山河呀
发布2025-01-26 17:27:55
发布2025-01-26 17:27:55
7900
代码可运行
举报
运行总次数:0
代码可运行
  • 本文的实际项目为 国内运营商IP段 (本项目在域名解析上提供了备份,主营国内站点,若遇服务器宕机或割接升级,会自动提供备份站点访问)
创建脚本

文件名随你的心意,并创建脚本基础

开头部分,基础格式以及基础数据的缓存路径和文件的命名
代码语言:javascript
代码运行次数:0
复制
#!/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/*#清空前一天的目录
创建分页文件,用于列举各个想要分类的运营商,文件名看自己心情,本文列举做到见名知意。
代码语言:javascript
代码运行次数:0
复制
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
根据第一部分已经做完的设定,处理数据。根据上述举例,处理属于电信的部分
代码语言:javascript
代码运行次数:0
复制
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
  • 上述为使用判断语句,将内容写入相应的分页文件,请注意,区分运营商越多,脚本越复杂,注意语句的完整性,脚本错误会导致运行失败,但结果可能不是马上就会显现。
  • 本处举例完整的体现了“中国电信”的IP是如何区分出来的(仅IPV4),如果需要获取IPV6的相关数据,脚本类似,正则表达部分略有不同。
继续完成分页文件的写入
代码语言:javascript
代码运行次数:0
复制
echo "###################  END  ###################<br><br>" >>${save_dir}/chinatelecom.html
echo '&copy;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
脚本结尾的处理
代码语言:javascript
代码运行次数:0
复制
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}/*

上述内容请自行观看,懒得码字了,抱一丝抱一丝

至此脚本举例结束

后记

本人实际项目中遇到以下问题,请各位大佬施以援手
  • 部分运营商使用netname会出现结果异常(不准确),采集到不属于该运营商的IP段,原因是netname存在包含关系(数学上叫真子集),例如CMCC与MMCMCC就是这样的关系,采集CMCC时后者也会被采集进去,造成结果不准,当前项目中仍有这样的问题,但已经尽力避免了。
  • 中国广电(原各地区广电网络)因历史原因,各地区的netname各不相同,因此脚本程度相当复杂(项目中实际共区分34个netname,真的要爆炸了)
  • 为了更直观的查看其他运营商和广电网络究竟是属于哪个单位的,因此增加了备注的输出(上文未提及,仅在实际项目中运用),所以在脚本处理上,做了分割,以做更详细的匹配(上文有提及,在第二部分)

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 创建脚本
  • 开头部分,基础格式以及基础数据的缓存路径和文件的命名
  • 创建分页文件,用于列举各个想要分类的运营商,文件名看自己心情,本文列举做到见名知意。
  • 根据第一部分已经做完的设定,处理数据。根据上述举例,处理属于电信的部分
  • 继续完成分页文件的写入
  • 脚本结尾的处理
  • 至此脚本举例结束
  • 后记
    • 本人实际项目中遇到以下问题,请各位大佬施以援手
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档