How to use the netaddr.valid_ipv4 function in netaddr

To help you get started, we’ve selected a few netaddr examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github projectcalico / calicoctl / calico_node / startup.py View on Github external
break

    # Query the currently configured host IPs
    try:
        current_ip, current_ip6 = client.get_host_bgp_ips(hostname)
    except KeyError:
        current_ip, current_ip6 = None, None

    # Determine the IP addresses and AS Number to use
    ip = os.getenv("IP") or None
    if ip == "autodetect":
        # If explicitly requesting auto-detection, set the ip to None to force
        # auto-detection.  We print below if we are auto-detecting the IP.
        ip = None
    elif ip:
        if not netaddr.valid_ipv4(ip):
            print "IP environment (%s) is not a valid IPv4 address." % ip
            sys.exit(1)
        print "Using IPv4 address from IP environment: %s" % ip
    elif current_ip:
        print "Using configured IPv4 address: %s" % current_ip
        ip = current_ip

    # Get IP address of host, if none was specified or previously configured.
    if not ip:
        ips = get_host_ips(exclude=["^docker.*", "^cbr.*", "dummy.*",
                                    "virbr.*", "lxcbr.*", "veth.*",
                                    "cali.*", "tunl.*", "flannel.*"])
        try:
            ip = str(ips.pop())
        except IndexError:
            print "Couldn't autodetect a management IPv4 address. Please " \
github cobbler / cobbler / cobbler / validate.py View on Github external
def ipv4_address(addr):
    """
    Validate an IPv4 address.

    @param: str addr (ipv4 address)
    @returns: str addr or CX
    """
    if not isinstance(addr, str):
        raise CX("Invalid input, addr must be a string")
    else:
        addr = addr.strip()

    if addr == "":
        return addr

    if not netaddr.valid_ipv4(addr):
        raise CX("Invalid IPv4 address format (%s)" % addr)

    if netaddr.IPAddress(addr).is_netmask():
        raise CX("Invalid IPv4 host address (%s)" % addr)

    return addr
github projectcalico / felix / python / calico / common.py View on Github external
def validate_ip_addr(addr, version=None):
    """
    Validates that an IP address is valid. Returns true if valid, false if
    not. Version can be "4", "6", None for "IPv4", "IPv6", or "either"
    respectively.
    """
    if version == 4:
        return netaddr.valid_ipv4(addr)
    elif version == 6:
        return netaddr.valid_ipv6(addr)
    else:
        return netaddr.valid_ipv4(addr) or netaddr.valid_ipv6(addr)
github zdresearch / OWASP-Nettacker / core / ip.py View on Github external
def isIP(IP):
    """
    to check a value if its IPv4 address

    Args:
        IP: the value to check if its IPv4

    Returns:
         True if it's IPv4 otherwise False
    """
    IP = str(IP)
    ip_flag=netaddr.valid_ipv4(IP)
    return ip_flag
github openstack / oslo.config / oslo_config / types.py View on Github external
def _check_ipv4(self, address):
        if not netaddr.valid_ipv4(address, netaddr.core.INET_PTON):
            raise ValueError("%s is not an IPv4 address" % address)
github IBM / power-up / scripts / python / ip_route_get_to.py View on Github external
def ip_route_get_to(host):
    """Get interface IP that routes to hostname or IP address

    Args:
        host (str): Hostname or IP address

    Returns:
        str: Interface IP with route to host
    """
    log = logger.getlogger()

    # Check if host is given as IP address
    if netaddr.valid_ipv4(host, flags=0):
        host_ip = host
    else:
        if host == socket.gethostname():
            host = 'localhost'
        try:
            host_ip = socket.gethostbyname(host)
        except socket.gaierror as exc:
            log.warning("Unable to resolve host to IP: '{}' exception: '{}'"
                        .format(host, exc))
    with IPRoute() as ipr:
        route = ipr.route('get', dst=host_ip)[0]['attrs'][3][1]

    return route
github openstack / tacker / tacker / common / utils.py View on Github external
def is_valid_ipv4(address):
    """Verify that address represents a valid IPv4 address."""
    try:
        return netaddr.valid_ipv4(address)
    except Exception:
        return False
github mozilla / MozDef / mq / plugins / squidFixup.py View on Github external
def isIPv4(self, ip):
        try:
            return valid_ipv4(ip)
        except:
            return False
github openmainframeproject / python-zvm-sdk / zvmsdk / api.py View on Github external
(network['ip_addr'] is not None)):
                ip_addr = network['ip_addr']
                if not netaddr.valid_ipv4(ip_addr):
                    errmsg = ("API guest_create_network_interface: "
                              "Invalid management IP address, it should be "
                              "the value between 0.0.0.0 and 255.255.255.255")
                    raise exception.SDKInvalidInputFormat(msg=errmsg)

            if (('dns_addr' in network.keys()) and
                (network['dns_addr'] is not None)):
                if not isinstance(network['dns_addr'], list):
                    raise exception.SDKInvalidInputTypes(
                        'guest_config_network',
                        str(list), str(type(network['dns_addr'])))
                for dns in network['dns_addr']:
                    if not netaddr.valid_ipv4(dns):
                        errmsg = ("API guest_create_network_interface: "
                                  "Invalid dns IP address, it should be the "
                                  "value between 0.0.0.0 and 255.255.255.255")
                        raise exception.SDKInvalidInputFormat(msg=errmsg)

            if (('gateway_addr' in network.keys()) and
                (network['gateway_addr'] is not None)):
                if not netaddr.valid_ipv4(
                                    network['gateway_addr']):
                    errmsg = ("API guest_create_network_interface: "
                              "Invalid gateway IP address, it should be "
                              "the value between 0.0.0.0 and 255.255.255.255")
                    raise exception.SDKInvalidInputFormat(msg=errmsg)
            if (('cidr' in network.keys()) and
                (network['cidr'] is not None)):
                if not zvmutils.valid_cidr(network['cidr']):
github projectcalico / felix / python / calico / common.py View on Github external
def validate_ip_addr(addr, version=None):
    """
    Validates that an IP address is valid. Returns true if valid, false if
    not. Version can be "4", "6", None for "IPv4", "IPv6", or "either"
    respectively.
    """
    if version == 4:
        return netaddr.valid_ipv4(addr)
    elif version == 6:
        return netaddr.valid_ipv6(addr)
    else:
        return netaddr.valid_ipv4(addr) or netaddr.valid_ipv6(addr)