spam_factory.py 文件源码

python
阅读 20 收藏 0 点赞 0 评论 0

项目:spamostack 作者: seecloud 项目源码 文件源码
def come_up_subnet(neutron_subnets, size):
    """Come ups subnets with specific size

    @param neutron_subnets: List of neutron subnets
    @type neutron_subnets: `list(client_factory.Accessible)`

    @param size: Size of future subnet
    @type size: `int`
    """

    log.info("Start generating of subnets")
    subnets = []

    for neutron_subnet in neutron_subnets:
        first = netaddr.IPAddress(netaddr.IPNetwork(neutron_subnet.cidr).first)
        last = netaddr.IPAddress(netaddr.IPNetwork(neutron_subnet.cidr).last)
        subnets.extend([first, last])

    subnets.sort()
    subnets.insert(0, netaddr.IPAddress("192.0.0.0"))
    subnets.append(netaddr.IPAddress("192.255.255.255"))

    max_size = 0
    for start, end in zip(subnets[::2], subnets[1::2]):
        space_range = list(netaddr.IPRange(start, end))
        if str(start) != "192.0.0.0":
            space_range = space_range[1:]
        elif str(end) != "192.255.255.255":
            space_range = space_range[:-1]

        space_size = len(space_range)
        if space_size > max_size:
            max_size = space_size
            start_ip = str(start)
            end_ip = str(end)
        if space_size >= size:
            return netaddr.iprange_to_cidrs(space_range[0],
                                            space_range[size - 1])[0]

    return netaddr.iprange_to_cidrs(start_ip, end_ip)[0]
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号