List of IP addresses in Python to a list of CIDR

后端 未结 6 1163
温柔的废话
温柔的废话 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条回答
  •  Happy的楠姐
    2021-01-03 05:18

    Well, summarize_address_range reduces your problem to splitting your list into consecutive ranges. Given that you can convert IP addresses to integers using

    def to_int(str): struct.unpack("!i",socket.inet_aton(str))[0]
    

    this should not be too hard.

提交回复
热议问题