将IP列表转换为相应的IP范围列表(python)

时间:2019-09-20 12:01:47

标签: python ip subnet

我想将IP列表转换为相应IP范围的列表。 例如:

iplist = ['137.226.161.121', '134.130.4.1', '137.226.161.149', '137.226.161.221', '137.226.161.240', '137.226.161.237', '8.8.8.8', '8.8.4.4', '137.226.161.189', '137.226.161.245', '137.226.161.172', '137.226.161.241', '137.226.161.234', '137.226.161.236', '134.130.5.1']

ipranges = ['137.226.161.0/24', '134.130.4.0/24', '8.8.8.0/24', '8.8.4.0/24', '134.130.5.0/24']

最有效的方法是什么?我还没有找到提供类似功能的模块。启用此功能的原因是,应将一长串IP(超过1000 ips)转换为子网列表,以提高可读性。

谢谢

2 个答案:

答案 0 :(得分:4)

如果我对您的理解正确,则只希望基于前24位(/ 24)相同进行匹配。对于这些任务,我建议使用set

iplist = ['137.226.161.121', '134.130.4.1', '137.226.161.149', '137.226.161.221', '137.226.161.240', '137.226.161.237', '8.8.8.8', '8.8.4.4', '137.226.161.189', '137.226.161.245', '137.226.161.172', '137.226.161.241', '137.226.161.234', '137.226.161.236', '134.130.5.1']

ipset = set()
for i in iplist:
    ipset.add(".".join(i.split(".")[:-1]))

ipranges = [p + ".0/24" for p in ipset]
print(ipranges)

打印: ['134.130.5.0/24', '8.8.4.0/24', '8.8.8.0/24', '134.130.4.0/24', '137.226.161.0/24']

那么这段代码是做什么的?

首先,我们遍历列表,并切断每个IP的最后一段:

segments = "8.8.8.8".split(".")  # segments == ["8", "8", "8", "8"]
segments_cut = segments[:-1]     # segments_cut == ["8", "8", "8"]
prefix = ".".join(segments_cut)  # prefix == "8.8.8"

现在,我们将这些前缀添加到set中。 Python set仅允许使用唯一元素。结果是:ìpset == {'134.130.5', '8.8.4', '8.8.8', '134.130.4', '137.226.161'}

最后,我们遍历集合并附加后缀“ .0 / 24”以表示子网。

编辑:关于“效率”

我喜欢answer by darkless,但只知道我的解决方案速度更快(1.2 s vs 0.09 s):

>>> import timeit
>>> # darkless' ipaddress solution
>>> timeit.timeit("[str(ipaddress.ip_network('{}/24'.format(ip), strict=False)) for ip in iplist]", setup="import ipaddress;iplist = ['137.226.161.121', '134.130.4.1', '137.226.161.149', '137.226.161.221', '137.226.161.240', '137.226.161.237', '8.8.8.8', '8.8.4.4', '137.226.161.189', '137.226.161.245', '137.226.161.172', '137.226.161.241', '137.226.161.234', '137.226.161.236', '134.130.5.1']", number=10000)
1.186...
>>> # My solution
>>> timeit.timeit("[p + '.0/24' for p in {'.'.join(i.split('.')[:-1]) for i in iplist}]", setup="import ipaddress;iplist = ['137.226.161.121', '134.130.4.1', '137.226.161.149', '137.226.161.221', '137.226.161.240', '137.226.161.237', '8.8.8.8', '8.8.4.4', '137.226.161.189', '137.226.161.245', '137.226.161.172', '137.226.161.241', '137.226.161.234', '137.226.161.236', '134.130.5.1']", number=10000)
0.096...

答案 1 :(得分:1)

正如Hampus Larsson所述,您可以使用python ipaddress模块​​:

import ipaddress

iplist = ['137.226.161.121', '134.130.4.1', '137.226.161.149', '137.226.161.221', '137.226.161.240', '137.226.161.237', '8.8.8.8', '8.8.4.4', '137.226.161.189', '137.226.161.245', '137.226.161.172', '137.226.161.241', '137.226.161.234', '137.226.161.236', '134.130.5.1']

ipranges = [str(ipaddress.ip_network('{}/24'.format(ip), strict=False)) for ip in iplist]

>>> ipranges
['137.226.161.0/24', '134.130.4.0/24', '137.226.161.0/24', '137.226.161.0/24', '137.226.161.0/24', '137.226.161.0/24', '8.8.8.0/24', '8.8.4.0/24', '137.226.161.0/24', '137.226.161.0/24', '137.226.161.0/24', '137.226.161.0/24', '137.226.161.0/24', '137.226.161.0/24', '134.130.5.0/24']