Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert CIDR to IP ranges using python3?

How to convert list like:

94.192.0.0/14  
94.0.0.0/12  
93.96.0.0/16 

To:

94.192.0.0-94.195.255.255  
94.0.0.0-94.15.255.255  
93.96.0.0-93.96.255.255  

Using python3?

like image 493
tellst1 Avatar asked May 16 '18 22:05

tellst1


1 Answers

Use the ipaddress builtin module:

>>> import ipaddress

>>> net=ipaddress.ip_network('94.192.0.0/14')
IPv4Network('94.192.0.0/14')

>>> '%s-%s' % (net[0], net[-1])
'94.192.0.0-94.195.255.255'

 

With for i in net you can also enumerate all ip addresses in the network net.

like image 115
fferri Avatar answered Sep 21 '22 02:09

fferri