如何从Python中的CIDR列表生成所有可能的IP? [英] How can I generate all possible IPs from a CIDR list in Python?
本文介绍了如何从Python中的CIDR列表生成所有可能的IP?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
假设我有一个文本文件包含一堆像这样的cidr ip范围:
Let's say I have a text file contains a bunch of cidr ip ranges like this:
x.x.x.x/24
x.x.x.x/24
x.x.x.x/23
x.x.x.x/23
x.x.x.x/22
x.x.x.x/22
x.x.x.x/21
继续......
如何将这些cidr符号转换为所有可能的ip在Python中的新文本文件中列出?
How can I convert these cidr notations to all possible ip list in a new text file in Python?
推荐答案
您可以使用netaddr进行此操作。下面的代码将在您的磁盘上创建一个文件并用所请求的块中的每个IP地址填充它:
You can use netaddr for this. The code below will create a file on your disk and fill it with every ip address in the requested block:
from netaddr import *
f = open("everyip.txt", "w")
ip = IPNetwork('10.0.0.0/8')
for addr in ip:
f.write(str(addr) + '\n')
f.close()
这篇关于如何从Python中的CIDR列表生成所有可能的IP?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文