Ejemplo n.º 1
0
    def _get_ip_data(block):
        """
        Construct an IPBlock message from a given IP block.

        Args:
            block (ipaddress.ip_network): the IP block to embed
        """
        ipblock_msg = IPBlock()
        ipblock_msg.version = IPBlock.IPV4
        ipblock_msg.net_address = block.network_address.packed
        ipblock_msg.prefix_len = block.prefixlen
        return ipblock_msg
Ejemplo n.º 2
0
def add_ip_block_handler(client, args):
    try:
        ipblock = ipaddress.ip_network(args.ipblock)
    except ValueError:
        print("Error: invalid IP block format: %s" % args.ipblock)
        return
    ipblock_msg = IPBlock()
    if ipblock.version == 4:
        ipblock_msg.version = IPBlock.IPV4
    else:
        print("Error: IP version %d is not supported yet" % ipblock.version)
        return
    ipblock_msg.net_address = ipblock.network_address.packed
    ipblock_msg.prefix_len = ipblock.prefixlen
    client.AddIPBlock(ipblock_msg)
Ejemplo n.º 3
0
def remove_ip_block_handler(client, args):
    ipblock_msgs = ()
    for ipblock in args.ipblocks:
        ipblock_msg = IPBlock()
        if ipblock.version == 4:
            ipblock_msg.version = IPBlock.IPV4
        else:
            print("Error: IP version %d is not supported yet" %
                  ipblock.version)
            return
        ipblock_msg.net_address = ipblock.network_address.packed
        ipblock_msg.prefix_len = ipblock.prefixlen
        ipblock_msgs += (ipblock_msg, )

    request = RemoveIPBlockRequest(ip_blocks=ipblock_msgs, force=args.force)
    remove_response = client.RemoveIPBlock(request)
    print("IPv4 Blocks Removed: ")
    for block_msg in remove_response.ip_blocks:
        ip = ipaddress.ip_address(block_msg.net_address)
        block = ipaddress.ip_network("%s/%d" % (ip, block_msg.prefix_len))
        print("\t%s" % block)