python网络地址库 netaddr 可自定义生成IP段

挺实用的,记录一下

https://netaddr.readthedocs.org/en/latest/

>>> for ip in IPNetwork('192.0.2.0/23').iter_hosts(): 
...     print '%s' % ip 
... 
192.0.2.1 
192.0.2.2 
192.0.2.3 
192.0.2.4 
... 
192.0.3.251 
192.0.3.252 
192.0.3.253 
192.0.3.254
>>> for ip in IPNetwork('192.0.2.0/23').iter_hosts(): 
...     print '%s' % ip 
... 
192.0.2.1 
192.0.2.2 
192.0.2.3 
192.0.2.4 
... 
192.0.3.251 
192.0.3.252 
192.0.3.253 
192.0.3.254

>>> r1 = IPRange('192.0.2.1', '192.0.2.15') 
>>> r1 
IPRange('192.0.2.1', '192.0.2.15')
>>> r1.cidrs() 
[IPNetwork('192.0.2.1/32'), IPNetwork('192.0.2.2/31'), IPNetwork('192.0.2.4/30'), IPNetwork('192.0.2.8/29')]
>>> subnets = list(ip.subnet(23)) 
>>> len(subnets) 
128 
>>> subnets 
[IPNetwork('172.24.0.0/23'), IPNetwork('172.24.2.0/23'), IPNetwork('172.24.4.0/23'),

 

link:http://zone.wooyun.org/content/23922

文 / admin
LEAVE A REPLY

loading