tellst1
tellst1

Reputation: 83

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?

Upvotes: 8

Views: 6576

Answers (1)

fferri
fferri

Reputation: 18940

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.

Upvotes: 12

Related Questions