Python ipaddr.IPv6Address() Examples

The following are 12 code examples of ipaddr.IPv6Address(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module ipaddr , or try the search function .
Example #1
Source File: url_helpers.py    From n6 with GNU Affero General Public License v3.0 6 votes vote down vote up
def _get_ipv6_addr(match):
    ipv6_main_part = match.group('ipv6_main_part')
    assert ipv6_main_part
    ipv6_suffix_in_ipv4_format = match.group('ipv6_suffix_in_ipv4_format')
    try:
        if ipv6_suffix_in_ipv4_format:
            assert ipv6_main_part.endswith(':')
            ipv6_suffix = _convert_ipv4_to_ipv6_suffix(ipv6_suffix_in_ipv4_format)
        else:
            assert ipv6_main_part == match.group('ipv6_addr')
            ipv6_suffix = ''
        ipv6_addr = ipaddr.IPv6Address(ipv6_main_part + ipv6_suffix).compressed
    except ipaddr.AddressValueError:
        ipv6_addr = match.group('ipv6_addr')
    assert is_pure_ascii(ipv6_addr)
    return ipv6_addr 
Example #2
Source File: attacks.py    From Chiron with GNU General Public License v3.0 6 votes vote down vote up
def neighbor_solicitation_spoofing(spoofed_source, target, myinterface, mac):
	solicited_node_multicast_address_prefix="ff02::1:ff"
	addr6 = ipaddr.IPv6Address(target)
	exploded=addr6.exploded
	length=len(exploded)
	suffix=exploded[(length-2):length]
	other=exploded[(length-4):(length-2)]
	the_other=exploded[(length-7):(length-5)]
	addresses={}
	#ns=ICMPv6ND_NS(tgt=target, R=0, S=0, O=1)/ICMPv6NDOptDstLLAddr(type=1,lladdr=mac)
	ns=scapy.layers.inet6.ICMPv6ND_NS(tgt=target)/scapy.layers.inet6.ICMPv6NDOptDstLLAddr(type=1,lladdr=mac)
	multi_address=solicited_node_multicast_address_prefix+the_other+":"+other+suffix
	packet=scapy.layers.inet6.IPv6(src=spoofed_source,dst=multi_address)/ns
	dest_multi_mac="33:33:ff:"+the_other+":"+other+":"+suffix
	ans,unan=scapy.sendrecv.srp(scapy.layers.l2.Ether(src=mac, dst=dest_multi_mac)/packet,iface=myinterface, timeout=2)
	for s,r in ans:
		try:
			addresses.update({r[IPv6].src:r[scapy.layers.l2.Ether].src})
		except:
			print "target",target, "was not found"
	return addresses 
Example #3
Source File: common_util.py    From encrypted-bigquery-client with Apache License 2.0 6 votes vote down vote up
def FormatPackedIP(packed_ip):
  """Formats packed binary data to a readable ip address.

  Args:
    packed_ip: The packed binary data to be converted.

  Returns:
    A readable ip address.

  Returns:
    bigquery_client.BigqueryInvalidQueryError: If the address is not valid.
  """
  packed_ip = ipaddr.Bytes(str(packed_ip))
  try:
    ip_address = ipaddr.IPv4Address(packed_ip)
    return str(ip_address)
  except ipaddr.AddressValueError as e:
    pass
  try:
    ip_address = ipaddr.IPv6Address(packed_ip)
    return str(ip_address)
  except ipaddr.AddressValueError as e:
    raise bigquery_client.BigqueryInvalidQueryError(e, None, None, None) 
Example #4
Source File: common_util.py    From encrypted-bigquery-client with Apache License 2.0 6 votes vote down vote up
def ParsePackedIP(readable_ip):
  try:
    ip_address = ipaddr.IPv4Address(readable_ip)
    return str(ipaddr.v4_int_to_packed(int(ip_address)))
  except ValueError:
    pass
  try:
    ip_address = ipaddr.IPv6Address(readable_ip)
    return str(ipaddr.v6_int_to_packed(int(ip_address)))
  except ValueError:
    raise bigquery_client.BigqueryInvalidQueryError(
        'Invalid readable ip.', None, None, None)


# TODO(user): Implement all URL functions.
# Supported URL functions. 
Example #5
Source File: url_helpers.py    From n6 with GNU Affero General Public License v3.0 5 votes vote down vote up
def _convert_ipv4_to_ipv6_suffix(ipv6_suffix_in_ipv4_format):
    """
    >>> _convert_ipv4_to_ipv6_suffix('192.168.0.1')
    'c0a8:0001'
    """
    as_ipv4 = ipaddr.IPv4Address(ipv6_suffix_in_ipv4_format)
    as_int = int(as_ipv4)
    as_ipv6 = ipaddr.IPv6Address(as_int)
    ipv6_suffix = as_ipv6.exploded[-9:]
    assert _LAST_9_CHARS_OF_EXPLODED_IPV6_REGEX.search(ipv6_suffix)
    return ipv6_suffix 
Example #6
Source File: fields.py    From n6 with GNU Affero General Public License v3.0 5 votes vote down vote up
def _fix_value(self, value):
        value = super(IPv6Field, self)._fix_value(value)
        try:
            ipv6_obj = ipaddr.IPv6Address(value)
        except Exception:
            raise FieldValueError(public_message=(
                self.error_msg_template.format(ascii_str(value))))
        return ipv6_obj 
Example #7
Source File: runtime_API.py    From p4-utils with GNU General Public License v2.0 5 votes vote down vote up
def ipv6Addr_to_bytes(addr):
    from ipaddr import IPv6Address
    if not ':' in addr:
        raise CLI_FormatExploreError()
    try:
        ip = IPv6Address(addr)
    except:
        raise UIn_BadIPv6Error()
    try:
        return [ord(b) for b in ip.packed]
    except:
        raise UIn_BadIPv6Error() 
Example #8
Source File: ccp_util.py    From ciscoconfparse with GNU General Public License v3.0 5 votes vote down vote up
def __init__(self, arg="::1/128", strict=False):

        # arg= _RGX_IPV6ADDR_NETMASK.sub(r'\1/\2', arg) # mangle IOS: 'addr mask'
        self.arg = arg
        self.dna = "IPv6Obj"

        try:
            mm = _RGX_IPV6ADDR.search(arg)
        except TypeError:
            if getattr(arg, "dna", "") == "IPv6Obj":
                ip_str = "{0}/{1}".format(str(arg.ip_object), arg.prefixlen)
                self.network_object = IPv6Network(ip_str, strict=False)
                self.ip_object = IPv6Address(str(arg.ip_object))
                return None
            elif isinstance(arg, IPv6Network):
                self.network_object = arg
                self.ip_object = IPv6Address(str(arg).split("/")[0])
                return None
            elif isinstance(arg, IPv6Address):
                self.network_object = IPv6Network(str(arg) + "/128")
                self.ip_object = IPv6Address(str(arg).split("/")[0])
                return None
            elif isinstance(arg, int):
                self.ip_object = IPv6Address(arg)
                self.network_object = IPv6Network(
                    str(self.ip_object) + "/128", strict=False
                )
                return None
            else:
                raise ValueError(
                    "IPv6Obj doesn't understand how to parse {0}".format(arg)
                )

        assert not (mm is None), "IPv6Obj couldn't parse {0}".format(arg)
        self.network_object = IPv6Network(arg, strict=strict)
        self.ip_object = IPv6Address(mm.group(1))

    # 'address_exclude', 'compare_networks', 'hostmask', 'ipv4_mapped', 'iter_subnets', 'iterhosts', 'masked', 'max_prefixlen', 'netmask', 'network', 'numhosts', 'overlaps', 'prefixlen', 'sixtofour', 'subnet', 'supernet', 'teredo', 'with_hostmask', 'with_netmask', 'with_prefixlen' 
Example #9
Source File: ccp_util.py    From ciscoconfparse with GNU General Public License v3.0 5 votes vote down vote up
def ip(self):
        """Returns the address as an IPv6Address object."""
        return self.ip_object 
Example #10
Source File: ccp_util.py    From ciscoconfparse with GNU General Public License v3.0 5 votes vote down vote up
def netmask(self):
        """Returns the network mask as an IPv6Address object."""
        return self.network_object.netmask 
Example #11
Source File: ccp_util.py    From ciscoconfparse with GNU General Public License v3.0 5 votes vote down vote up
def hostmask(self):
        """Returns the host mask as an IPv6Address object."""
        return self.network_object.hostmask 
Example #12
Source File: bytes_utils.py    From p4runtime-shell with Apache License 2.0 5 votes vote down vote up
def ipv6Addr_to_bytes(addr):
    try:
        ip = IPv6Address(addr)
    except AddressValueError:
        raise UserBadIPv6Error(addr)
    return ip.packed