List of IP addresses in Python to a list of CIDR

后端 未结 6 1052
温柔的废话
温柔的废话 2021-01-03 04:21

How do I convert a list of IP addresses to a list of CIDRs? Google\'s ipaddr-py library has a method called summarize_address_range(first, last) that converts two IP addres

6条回答
  •  别那么骄傲
    2021-01-03 05:00

    in python3, we have a buildin module for this: ipaddress.

    list_of_ips = ['10.0.0.0', '10.0.0.1', '10.0.0.2', '10.0.0.3', '10.0.0.5']
    import ipaddress
    nets = [ipaddress.ip_network(_ip) for _ip in list_of_ips]
    cidrs = ipaddress.collapse_addresses(nets)
    list(cidrs)
    Out[6]: [IPv4Network('10.0.0.0/30'), IPv4Network('10.0.0.5/32')]
    

提交回复
热议问题