阿里云IP段的整理

阿里云IP段的整理

=Start=

缘由:

分析整理一下阿里云的IP列表,方便做一些分析。同时也简单记录一下处理过程中遇到的几个觉得有意思的点,避免遗忘。

正文:

参考解答:

简单来说,想要整理出一个较为完整的阿里云IP段,主要有2种思路:

有钱的话,建议买类似于ipip.net这种专门团队维护的的付费库,里面有标明;没钱的话,去分析ASN的数据进行提取,或是从网上找一找(但从网上找的这个准确性就没人能保证了)。

全球互联网,实际上由一个个自治系统(AS, Autonomous System)连接而成。自治系统内可以由许许多多路由器组成。每个自治系统在全球有一个唯一编号(ASN, Autonomous System Number)。

这里出于简单可维护起见,使用了付费版的ipip.net的数据(txt版本)。样例格式如下:

008.128.000.000 008.128.003.255 中国 上海 上海 * 阿里云/电信/联通/移动/铁通/教育网 31.224349 121.476753 Asia/Shanghai UTC+8 310000 86 CN AP IDC * CHN 0

008.128.004.000 008.128.255.255 中国 中国 * * 阿里云/电信/联通/移动/铁通/教育网 36.894402 104.166 Asia/Shanghai UTC+8 100000 86 CN AP IDC * CHN 0

008.129.000.000 008.129.255.255 中国 广东 深圳 * 阿里云/电信/联通/移动/铁通/教育网 22.55516 114.053879 Asia/Shanghai UTC+8 440300 86 CN AP IDC * CHN 0

根据下面两列进行关键字匹配,即可拿到阿里云的IP段,然后再进行一个计算,即可获取里面包含的全量阿里云IP信息。

owner_domain : 所有者(每周高级版及其以上版本包含) #第6列isp_domain : 运营商(每周高级版与每日高级版及其以上版本包含) #第7列

第1列、第2列是IP的起止地址。但有一个问题在于,【008.128.000.000】这种格式不被其它人(IP格式处理库)所识别和认可,需要转换成【8.128.0.0】这种格式才行。

这里就涉及到几个点:

【整数含义字符串】中前导0的去除;将IP段进行合并然后转换成具体的IP列表(方便直接对比)。

一、Python中如何优雅的进行 前导0 的去除?

# 方法一 带参数的lstrip

>>> '00000010'.lstrip('0')

'10'

# 方法二 正则表达式

>>> re.sub(r'0+(.+)', r'\1', '000010')

'10'

>>> re.sub(r'0+(.+)', r'\1', '0')

'0'

# 方法三 用 int 进行转换

>>> str(int('0000010'))

'10'

# 其他方法

>>> s = '000010'

>>> s[:-1].lstrip('0') + s[-1]

二、将IP段转换成CIDR或是IP列表的格式

from netaddr import *

'''

ip1 = '008.128.000.000'

ip2_list = []

for item in ip1.split('.'):

ip2_list.append(str(int(item)))

#

ip2_str = '.'.join(ip2_list)

'''

def ip_format(ip_str):

var_list = []

for item in ip_str.split('.'):

var_list.append(str(int(item)))

return '.'.join(var_list)

def main():

ipset = IPSet()

with open('aliyun-ip.txt') as fp:

for line in fp:

line = line.strip()

if line and line.count('\t') == 18:

ip_start, ip_end, others = line.split('\t', 2)

ipset.add(IPRange(ip_format(ip_start), ip_format(ip_end)))

#

#

#

# for item in ipset.iter_ipranges():

# print item

# #

for item in ipset:

print item

# main

参考链接:

阿里云ip段https://blog.csdn.net/eagle_min/java/article/details/82260622

全球网络公司IP地址、IP地址段、互联网公司AS号码http://as.chacuo.net/company

http://as.chacuo.net/CN

放行WAF回源IP段https://www.alibabacloud.com/help/zh/doc-detail/99399.htm

谁有阿里云所有的IP段https://developer.aliyun.com/ask/245281

阿里云IP地址有哪些?阿里云IP地址段https://www.shangyunbang.com/eip/228.html

在哪里能下载到阿里云腾讯云所有IP段?https://www.hostloc.com/thread-451015-1-1.html

阿里云(aliyun)、腾讯云(qcloud)、百度云等常用CDN节点IP段整理(2019年12月011日更新)https://www.laoliang.net/jsjh/news/5835.html

屏蔽阿里云的所有 IP 的 Nginx 配置,防止阿里云员工审查你的网站https://www.v2ex.com/t/130672

https://stackoverflow.com/questions/13142347/how-to-remove-leading-and-trailing-zeros-in-a-string-python

https://stackoverflow.com/questions/53654080/remove-the-leading-zero-before-a-number-in-python

https://stackoverflow.com/questions/6708272/list-of-ip-addresses-in-python-to-a-list-of-cidr

https://netaddr.readthedocs.io/en/latest/tutorial_03.html#adding-and-removing-set-elements

https://netaddr.readthedocs.io/en/latest/tutorial_01.html?highlight=IPRange#arbitrary-ip-address-ranges

=END=

相关推荐