Search code examples
pythonpython-3.xipcidr

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?


Solution

  • 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.