Exemplo n.º 1
0
 def find_from_tuple(cls, link):
     """
     Find link by providing a tuple with two ip addresses or two mac addresses
     :param link: tuple with two string elements indicating source and destination (ip or mac addresses)
     :returns: Link object
     """
     try:
         a = link[0]
         b = link[1]
     except IndexError:
         raise ValueError('Expecting tuple with source and destination')
     # find interfaces
     if (valid_ipv4(a) and valid_ipv4(b)) or (valid_ipv6(a) and valid_ipv6(b)):
         try:
             a = Ip.objects.get(address=a).interface
             b = Ip.objects.get(address=b).interface
         except Ip.DoesNotExist as e:
             raise LinkDataNotFound(e)
     elif valid_mac(a) and valid_mac(b):
         try:
             a = Interface.objects.get(mac=a)
             b = Interface.objects.get(mac=b)
         except Interface.DoesNotExist as e:
             raise LinkDataNotFound(e)
     else:
         raise ValueError('Expecting valid ipv4, ipv6 or mac address')
     # find link with interfaces
     # inverse order is also ok
     q = Q(interface_a=a, interface_b=b) | Q(interface_a=b, interface_b=a)
     link = Link.objects.filter(q).first()
     if link is None:
         raise LinkNotFound('Link matching query does not exist',
                            interface_a=a,
                            interface_b=b)
     return link
Exemplo n.º 2
0
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)
Exemplo n.º 3
0
    def neighbor_add(self, address, remote_as,
                     enable_ipv4=DEFAULT_CAP_MBGP_IPV4,
                     enable_vpnv4=DEFAULT_CAP_MBGP_VPNV4,
                     enable_vpnv6=DEFAULT_CAP_MBGP_VPNV6,
                     next_hop=None, password=None, multi_exit_disc=None):
        """ This method registers a new neighbor. The BGP speaker tries to
        establish a bgp session with the peer (accepts a connection
        from the peer and also tries to connect to it).

        ``address`` specifies the IP address of the peer. It must be
        the string representation of an IP address. Only IP v4 is
        supported now.

        ``remote_as`` specifies the AS number of the peer. It must be
        an integer between 1 and 65535.

        ``enable_ipv4`` enables IPv4 address family for this
        neighbor. The default is True.

        ``enable_vpnv4`` enables VPNv4 address family for this
        neighbor. The default is False.

        ``enable_vpnv6`` enables VPNv6 address family for this
        neighbor. The default is False.

        ``next_hop`` specifies the next hop IP address. If not
        specified, host's ip address to access to a peer is used.

        ``password`` is used for the MD5 authentication if it's
        specified. By default, the MD5 authenticaiton is disabled.

        ``multi_exit_disc`` specifies multi exit discriminator (MED) value.
        The default is None and if not specified, MED value is
        not sent to the neighbor. It must be an integer.

        """
        bgp_neighbor = {}
        bgp_neighbor[neighbors.IP_ADDRESS] = address
        bgp_neighbor[neighbors.REMOTE_AS] = remote_as
        bgp_neighbor[PEER_NEXT_HOP] = next_hop
        bgp_neighbor[PASSWORD] = password
        # v6 advertizement is available with only v6 peering
        if netaddr.valid_ipv4(address):
            bgp_neighbor[CAP_MBGP_IPV4] = enable_ipv4
            bgp_neighbor[CAP_MBGP_IPV6] = False
            bgp_neighbor[CAP_MBGP_VPNV4] = enable_vpnv4
            bgp_neighbor[CAP_MBGP_VPNV6] = enable_vpnv6
        elif netaddr.valid_ipv6(address):
            bgp_neighbor[CAP_MBGP_IPV4] = False
            bgp_neighbor[CAP_MBGP_IPV6] = True
            bgp_neighbor[CAP_MBGP_VPNV4] = False
            bgp_neighbor[CAP_MBGP_VPNV6] = False
        else:
            # FIXME: should raise an exception
            pass

        if multi_exit_disc:
            bgp_neighbor[MULTI_EXIT_DISC] = multi_exit_disc

        call('neighbor.create', **bgp_neighbor)
Exemplo n.º 4
0
 def _format_address_for_dnsmasq(address):
     # (dzyu) Check if it is legal ipv6 address, if so, need wrap
     # it with '[]' to let dnsmasq to distinguish MAC address from
     # IPv6 address.
     if netaddr.valid_ipv6(address):
         return '[%s]' % address
     return address
Exemplo n.º 5
0
  def getAddress(self, allow_tap=False):
    """
    Return a list of the interface address not attributed to any partition, (which
    are therefore free for the computer itself).

    Returns:
      False if the interface isn't available, else the list of the free addresses.
    """
    if self.interface is None:
      return dict(addr=self.address, netmask=self.netmask)

    computer_partition_address_list = []
    for partition in self.partition_list:
      for address in partition.address_list:
        if netaddr.valid_ipv6(address['addr']):
          computer_partition_address_list.append(address['addr'])
    # Going through addresses of the computer's interface
    for address_dict in self.interface.getGlobalScopeAddressList():
      # Comparing with computer's partition addresses
      if address_dict['addr'] not in computer_partition_address_list:
        return address_dict

    if allow_tap:
      # all addresses on interface are for partition, so lets add new one
      computer_tap = Tap('compdummy')
      computer_tap.createWithOwner(User('root'), attach_to_tap=True)
      self.interface.addTap(computer_tap)
      return self.interface.addAddr()

    # Can't find address
    raise NoAddressOnInterface('No valid IPv6 found on %s.' %
        self.interface.name)
Exemplo n.º 6
0
    def is_ip(self, ip_addr=None):
        """
        Return true if valid IP address return false if invalid IP address
        :param ip_addr: optional IP to pass. Takes from root class if not specified
        >>> from ipinformation import IPInformation
        >>> print IPInformation(ip_address='8.8.8.8').is_ip()
            True
        >>> print IPInformation(ip_address='NotAnIP').is_ip()
            False
        """
        if not ip_addr:
            ip_addr = self.ip_address

        valid = True

        if netaddr.valid_ipv4(ip_addr):  # IPv4 Address
            if not re.match(valid_ip_regex, ip_addr):
                valid = False

        elif netaddr.valid_ipv6(ip_addr):
            pass

        else:
            # print '"%s" is not a valid IP Address.' %ip_addr
            valid = False

        return valid
Exemplo n.º 7
0
    def _is_pingable(self, mgmt_ip="", count=5, timeout=1, interval='0.2',
                     **kwargs):
        """Checks whether an IP address is reachable by pinging.

        Use linux utils to execute the ping (ICMP ECHO) command.
        Sends 5 packets with an interval of 0.2 seconds and timeout of 1
        seconds. Runtime error implies unreachability else IP is pingable.
        :param ip: IP to check
        :return: bool - True or string 'failure' depending on pingability.
        """
        cmd_ping = 'ping'
        if netaddr.valid_ipv6(mgmt_ip):
            cmd_ping = 'ping6'

        ping_cmd = [cmd_ping,
                    '-c', count,
                    '-W', timeout,
                    '-i', interval,
                    mgmt_ip]

        try:
            linux_utils.execute(ping_cmd, check_exit_code=True)
            return True
        except RuntimeError:
            LOG.warning("Cannot ping ip address: %s", mgmt_ip)
            return 'failure'
Exemplo n.º 8
0
 def _build_base_url(self, scheme):
     proto_str = "%s://" % scheme
     host_str = ("[%s]" % self._host if netaddr.valid_ipv6(self._host)
                 else self._host)
     port_str = ":%d" % (self._api_ssl_port if scheme == "https"
                         else self._api_port)
     return proto_str + host_str + port_str
Exemplo n.º 9
0
def run(args):
  
  # minimal web server.  serves files relative to the
  # current directory.
  logging.basicConfig(format="%(asctime)s - %(name)s - %(levelname)s - %(message)s",
                            filename=args['log-file'] ,level=logging.INFO)
  
  port = args['port']
  host = args['host']
  os.chdir(args['cwd'])
  
  Handler = ServerHandler
  Handler.document_path = args['root-dir']
  Handler.restrict_root_folder = (args['root-dir'] != args['cwd'])
  
  if valid_ipv6(host):
    server = HTTPServerV6
  else:
    server = HTTPServer
  
  httpd = server((host, port), Handler)
  scheme = 'http'
  if args.has_key('cert-file') and args.has_key('key-file') and \
      os.path.exists(args['cert-file']) and os.path.exists(args['key-file']):
    scheme = 'https'
    httpd.socket = ssl.wrap_socket (httpd.socket, 
                                     server_side=True,
                                     certfile=args['cert-file'],
                                     keyfile=args['key-file'])

  logging.info("Starting simple http server at %s://%s:%s" % (scheme, host, port))
  httpd.serve_forever()
Exemplo n.º 10
0
    def _process_networks(osutils, network_details):
        reboot_required = False
        ipv4_ns, ipv6_ns = NetworkConfigPlugin._get_default_dns_nameservers(
            network_details)

        for net in network_details.networks:
            ip_address, prefix_len = net.address_cidr.split("/")

            gateway = None
            default_gw_route = [
                r for r in net.routes if
                netaddr.IPNetwork(r.network_cidr).prefixlen == 0]
            if default_gw_route:
                gateway = default_gw_route[0].gateway

            nameservers = net.dns_nameservers
            if not nameservers:
                if netaddr.valid_ipv6(ip_address):
                    nameservers = ipv6_ns
                else:
                    nameservers = ipv4_ns

            LOG.info(
                "Setting static IP configuration on network adapter "
                "\"%(name)s\". IP: %(ip)s, prefix length: %(prefix_len)s, "
                "gateway: %(gateway)s, dns: %(dns)s",
                {"name": net.link, "ip": ip_address, "prefix_len": prefix_len,
                 "gateway": gateway, "dns": nameservers})
            reboot = osutils.set_static_network_config(
                net.link, ip_address, prefix_len, gateway, nameservers)
            reboot_required = reboot or reboot_required

        return reboot_required
Exemplo n.º 11
0
def create_connection(address):
    """
    Wrapper for socket.create_connection() function.

    If *address* (a 2-tuple ``(host, port)``) contains a valid IPv4/v6
    address, passes *address* to socket.create_connection().
    If *host* is valid path to Unix Domain socket, tries to connect to
    the server listening on the given socket.

    :param address: IP address or path to Unix Domain socket.
    :return: Socket instance.
    """
    host, _port = address

    if (netaddr.valid_ipv4(host)
            or netaddr.valid_ipv6(host)):
        return socket.create_connection(address)
    elif os.path.exists(host):
        sock = None
        try:
            sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
            sock.connect(host)
        except socket.error as e:
            if sock is not None:
                sock.close()
            raise e
        return sock
    else:
        raise ValueError('Invalid IP address or Unix Socket: %s' % host)
Exemplo n.º 12
0
    def take_action(self, args):
        configp = self.fetch_config(args)
        instance_root = configp.get('slapos','instance_root')
        master_url = urlparse(configp.get('slapos','master_url'))
        master_hostname = master_url.hostname

        # Check that we have IPv6 ready
        if configp.has_option('slapformat', 'ipv6_interface'):
            ipv6_interface = configp.get('slapformat', 'ipv6_interface')
        else:
            ipv6_interface = configp.get('slapformat', 'interface_name')
        _waitIpv6Ready(ipv6_interface)

        # Check that node can ping master
        if valid_ipv4(master_hostname):
          _test_ping(master_hostname)
        elif valid_ipv6(master_hostname):
          _test_ping6(master_hostname)
        else:
          # hostname
          _ping_hostname(master_hostname)

        app = SlapOSApp()
        # Make sure slapos node format returns ok
        while not _runFormat(app):
            print("[BOOT] [ERROR] Fail to format, try again in 15 seconds...")
            sleep(15)
       
        # Make sure slapos node bang returns ok
        while not _runBang(app):
            print("[BOOT] [ERROR] Fail to bang, try again in 15 seconds...")
            sleep(15)

        _removeTimestamp(instance_root)
Exemplo n.º 13
0
    def request(self, url, method='GET', body=None, headers=None,
                ssl_verify=True, stream=False):
        _headers = {'Content-Type': 'application/json'}
        _headers.update(headers or {})

        parsed_url = urlparse.urlparse(url)
        port = parsed_url.port
        hostname = parsed_url.hostname
        scheme = parsed_url.scheme

        if netaddr.valid_ipv6(hostname):
            hostname = "[%s]" % hostname

        relative_url = parsed_url.path
        if parsed_url.query:
            relative_url = relative_url + "?" + parsed_url.query
        LOG.info(_("Doing %(method)s on %(relative_url)s"),
                 {'method': method, 'relative_url': relative_url})
        if body:
            LOG.info(_("Body: %s") % body)

        if port:
            _url = "%s://%s:%d%s" % (scheme, hostname, int(port), relative_url)
        else:
            _url = "%s://%s%s" % (scheme, hostname, relative_url)

        response = requests.request(method, _url, data=body, headers=_headers,
                                    verify=ssl_verify, stream=stream)

        return response
Exemplo n.º 14
0
def parse_server_string(server_str):
    """Parses the given server_string and returns a tuple of host and port.
    If it's not a combination of host part and port, the port element
    is an empty string. If the input is invalid expression, return a tuple of
    two empty strings.
    """
    try:
        # First of all, exclude pure IPv6 address (w/o port).
        if netaddr.valid_ipv6(server_str):
            return (server_str, "")

        # Next, check if this is IPv6 address with a port number combination.
        if server_str.find("]:") != -1:
            (address, port) = server_str.replace("[", "", 1).split("]:")
            return (address, port)

        # Third, check if this is a combination of an address and a port
        if server_str.find(":") == -1:
            return (server_str, "")

        # This must be a combination of an address and a port
        (address, port) = server_str.split(":")
        return (address, port)

    except (ValueError, netaddr.AddrFormatError):
        LOG.error(_LE("Invalid server_string: %s"), server_str)
        return ("", "")
Exemplo n.º 15
0
def parse_server_string(server_str):
    """
    Parses the given server_string and returns a list of host and port.
    If it's not a combination of host part and port, the port element
    is a null string. If the input is invalid expression, return a null
    list.
    """
    try:
        # First of all, exclude pure IPv6 address (w/o port).
        if netaddr.valid_ipv6(server_str):
            return (server_str, '')

        # Next, check if this is IPv6 address with a port number combination.
        if server_str.find("]:") != -1:
            (address, port) = server_str.replace('[', '', 1).split(']:')
            return (address, port)

        # Third, check if this is a combination of an address and a port
        if server_str.find(':') == -1:
            return (server_str, '')

        # This must be a combination of an address and a port
        (address, port) = server_str.split(':')
        return (address, port)

    except Exception:
        LOG.error(_('Invalid server_string: %s'), server_str)
        return ('', '')
Exemplo n.º 16
0
    def prefix_del(self, prefix, route_dist=None):
        """ This method deletes a advertized prefix.

        ``prefix`` must be the string representation of an IP network
        (e.g., 10.1.1.0/24).

        ``route_dist`` specifies a route distinguisher value. This
        parameter is necessary for only VPNv4 and VPNv6 address
        families.

        """
        func_name = 'network.del'
        networks = {}
        networks[PREFIX] = prefix
        if route_dist:
            func_name = 'prefix.delete_local'
            networks[ROUTE_DISTINGUISHER] = route_dist

            ip, masklen = prefix.split('/')
            if netaddr.valid_ipv6(ip):
                networks[ROUTE_FAMILY] = vrfs.VRF_RF_IPV6
                # normalize IPv6 address expression
                networks[PREFIX] = \
                    str(netaddr.IPAddress(ip)) + '/' + masklen
            else:
                networks[ROUTE_FAMILY] = vrfs.VRF_RF_IPV4

        call(func_name, **networks)
 def _handle_host_snat_ip(self, host_snat_ips):
     for hsi in host_snat_ips:
         LOG.debug(_("Auto-allocated host SNAT IP: %s"), hsi)
         es = hsi.get('external_segment_name')
         if not es:
             continue
         nh = self.ext_seg_next_hop.setdefault(es, ExtSegNextHopInfo(es))
         if nh.from_config:
             continue    # ignore auto-allocation if manually set
         ip = hsi.get('host_snat_ip')
         gw = ("%s/%s" % (hsi['gateway_ip'], hsi['prefixlen'])
             if (hsi.get('gateway_ip') and hsi.get('prefixlen')) else None)
         updated = False
         if netaddr.valid_ipv4(ip):
             if ip != nh.ip_start or gw != nh.ip_gateway:
                 nh.ip_start = ip
                 nh.ip_gateway = gw
                 updated = True
         elif netaddr.valid_ipv6(ip):
             if ip != nh.ip6_start or gw != nh.ip6_gateway:
                 nh.ip6_start = ip
                 nh.ip6_gateway = gw
                 updated = True
         else:
             LOG.info(_("Ignoring invalid auto-allocated SNAT IP %s"), ip)
         if updated:
             # Clear the interface so that SNAT iptables will be
             # re-created as required; leave MAC as is so that it will
             # be re-used
             nh.next_hop_iface = None
             LOG.info(_("Add/update SNAT info: %s"), nh)
Exemplo n.º 18
0
    def _output_hosts_file(self):
        """Writes a dnsmasq compatible hosts file."""
        r = re.compile('[:.]')
        buf = six.StringIO()

        for port in self.network.ports:
            for alloc in port.fixed_ips:
                name = 'host-%s.%s' % (r.sub('-', alloc.ip_address),
                                       self.conf.dhcp_domain)
                set_tag = ''
                # (dzyu) Check if it is legal ipv6 address, if so, need wrap
                # it with '[]' to let dnsmasq to distinguish MAC address from
                # IPv6 address.
                ip_address = alloc.ip_address
                if netaddr.valid_ipv6(ip_address):
                    ip_address = '[%s]' % ip_address
                if getattr(port, 'extra_dhcp_opts', False):
                    if self.version >= self.MINIMUM_VERSION:
                        set_tag = 'set:'

                    buf.write('%s,%s,%s,%s%s\n' %
                              (port.mac_address, name, ip_address,
                               set_tag, port.id))
                else:
                    buf.write('%s,%s,%s\n' %
                              (port.mac_address, name, ip_address))

        name = self.get_conf_file_name('host')
        utils.replace_file(name, buf.getvalue())
        return name
Exemplo n.º 19
0
    def is_connective(self, ping_timeout=20.0):
        """
        Check if host network is connective via ping command

        :param ping_timeout: time to wait for response
        :type ping_timeout: float
        :return: True if address is connective via ping command,
            False otherwise
        :rtype: bool
        """
        host_address = self.host.ip
        # Leave it for future support of IPV6
        ping_cmd = "ping6" if netaddr.valid_ipv6(self.host.ip) else "ping"
        self.logger.info(
            "Check if address is connective via ping in given timeout %s",
            ping_timeout
        )
        command = [
            ping_cmd,
            "-c", "1",
            "-w", str(ping_timeout),
            host_address
        ]
        p = subprocess.Popen(
            command, stdout=subprocess.PIPE, stderr=subprocess.PIPE
        )
        out, _ = p.communicate()
        if p.returncode:
            self.logger.debug(
                "Failed to ping address %s: %s", host_address, out
            )
            return False
        return True
Exemplo n.º 20
0
def validate_ip_addr(ip_addr):
    if netaddr.valid_ipv4(ip_addr):
        return lib_consts.IP_VERSION_4
    elif netaddr.valid_ipv6(ip_addr):
        return lib_consts.IP_VERSION_6
    else:
        raise bgp_driver_exc.InvalidParamType(param=ip_addr,
                                              param_type='ip-address')
Exemplo n.º 21
0
Arquivo: utils.py Projeto: aawm/manila
def is_valid_ip_address(ip_address, ip_version):
    if int(ip_version) == 4:
        return netaddr.valid_ipv4(ip_address)
    elif int(ip_version) == 6:
        return netaddr.valid_ipv6(ip_address)
    else:
        raise exception.ManilaException(
            _("Provided improper IP version '%s'.") % ip_version)
Exemplo n.º 22
0
def isIP(ip):
    try:
        if netaddr.valid_ipv4(ip,flags=netaddr.INET_PTON) or netaddr.valid_ipv6(ip,flags=netaddr.INET_PTON):
            return True
        else:
            return False
    except:
        return False
Exemplo n.º 23
0
  def __init__(self, buildout, name, options):
      slap = slapos.slap.slap()
      slap.initializeConnection(
          options['url'],
          options.get('key'),
          options.get('cert'),
      )
      computer_partition = slap.registerComputerPartition(
          options['computer'],
          options['partition'],
      )
      parameter_dict = computer_partition.getInstanceParameterDict()
      options['instance-state'] = computer_partition.getState()
      # XXX: those are not partition parameters, strictly speaking.
      # Make them available as individual section keys.
      for his_key in (
                  'slap_software_type',
                  'slap_computer_partition_id',
                  'slap_computer_id',
                  'slap_software_release_url',
                  'slave_instance_list',
                  'timestamp',
              ):
          try:
              value = parameter_dict.pop(his_key)
          except KeyError:
              pass
          else:
              options[his_key.replace('_', '-')] = value
      ipv4_set = set()
      v4_add = ipv4_set.add
      ipv6_set = set()
      v6_add = ipv6_set.add
      tap_set = set()
      tap_add = tap_set.add
      for tap, ip in parameter_dict.pop('ip_list'):
          tap_add(tap)
          if valid_ipv4(ip):
              v4_add(ip)
          elif valid_ipv6(ip):
              v6_add(ip)
          # XXX: emit warning on unknown address type ?
      options['ipv4'] = ipv4_set
      options['ipv6'] = ipv6_set

      # also export single ip values for those recipes that don't support sets.
      if ipv4_set:
          options['ipv4-random'] = list(ipv4_set)[0].encode('UTF-8')
      if ipv6_set:
          options['ipv6-random'] = list(ipv6_set)[0].encode('UTF-8')

      options['tap'] = tap_set
      parameter_dict = self._expandParameterDict(options, parameter_dict)
      match = self.OPTCRE_match
      for key, value in parameter_dict.iteritems():
          if match(key) is not None:
              continue
          options['configuration.' + key] = value
Exemplo n.º 24
0
    def general_info(self):
        """
        Return IP in bits, ip_type (ie: private, multicast, loopback,etc..), time updated/returned and version for an IP Address
        >>> from ipinformation import IPInformation
        >>> from pprint import pprint
        >>> pprint( IPInformation(ip_address='8.8.8.8').general_info() )
        {'general': {'bits': '00001000000010000000100000001000',
                     'type': 'public',
                     'updated': datetime.datetime(2016, 1, 16, 18, 7, 4, 288512),
                     'version': '4'}}
        >>> pprint( IPInformation(ip_address='127.0.0.1').general_info() )
        {'general': {'bits': '01111111000000000000000000000001',
                     'type': 'loopback',
                     'updated': datetime.datetime(2016, 1, 16, 18, 10, 6, 729149),
                     'version': '4'}}
        """
        data = {"general": {"bits": None, "type": None, "updated": None, "version": None}}

        if not self.ISIP:
            # print '"%s" is not a valid IP Address.' %self.ip_address
            # logging_file.error( '"{0}" is not a valid IP Address.'.format(self.ip_address) )
            return data

        if netaddr.valid_ipv4(self.ip_address):  # IPv4 Address
            ip_version = "4"
            data["general"].update({"version": ip_version})
            ip_bits = (
                netaddr.IPAddress(self.ip_address).bits().replace(".", "")
            )  # Set the IP bits for searching by subnet
            data["general"].update({"bits": ip_bits})
            ip_addr = netaddr.IPAddress(self.ip_address)

            if ip_addr.is_private():
                ip_type = "private"
            elif ip_addr.is_multicast():
                ip_type = "multicast"
            elif ip_addr.is_loopback():
                ip_type = "loopback"
            elif ip_addr.is_netmask():
                ip_type = "netmask"
            elif ip_addr.is_reserved():
                ip_type = "reserved"
            elif ip_addr.is_link_local():
                ip_type = "link_local"
            elif ip_addr.is_unicast():
                ip_type = "public"
            else:  # Unknown Type
                ip_type = "unknown"
                logging_file.error('"{0}" is an unknown IP Address.'.format(self.ip_address))
        elif netaddr.valid_ipv6(self.ip_address):  # IPv6 Address#TODO:Finish IPv6
            ip_version = "6"
            print "Is IPv6"
            return False

        data["general"].update({"type": ip_type})
        data["general"].update({"updated": datetime.utcnow()})
        return data
Exemplo n.º 25
0
def detect_address_family(host):
    if netaddr.valid_ipv4(host):
        return socket.AF_INET
    elif netaddr.valid_ipv6(host):
        return socket.AF_INET6
    elif os.path.isdir(os.path.dirname(host)):
        return socket.AF_UNIX
    else:
        return None
Exemplo n.º 26
0
    def _parse_hosts(self, hosts):
        """Parses the list of hosts and creates corresponding objects."""

        ip_list = [str(ip) for ip in list(netaddr.IPNetwork(hosts))]
        if netaddr.valid_ipv4(ip_list[0]):
            self.inet = socket.AF_INET
        elif netaddr.valid_ipv6(ip_list[0]):
            self.inet = socket.AF_INET6
        return [Host(ip, self.ports) for ip in ip_list]
Exemplo n.º 27
0
def valid_ipv6_url(host, port):
    """Given a host and a port returns a valid URL
       RFC2732 https://tools.ietf.org/html/rfc2732
       square brackets always required in ipv6 URI.
    """
    if netaddr.valid_ipv6(host):
        uri = '[%s]:%s' % (host, port)
    else:
        uri = '%s:%s' % (host, port)
    return uri
Exemplo n.º 28
0
def is_ip(string):
    try:
        if netaddr.valid_ipv4(string):
            return True
        if netaddr.valid_ipv6(string):
            return True
        return False
    except:
        traceback.print_exc()
        return False
Exemplo n.º 29
0
def is_ip(string):
    try:
        if netaddr.valid_ipv4(string):
            return True
        if netaddr.valid_ipv6(string):
            return True
        return False
    except:
        if logger.debug:
            traceback.print_exc()
        return False
Exemplo n.º 30
0
 def _get_link_interface(self, string_id):
     if valid_ipv4(string_id) or valid_ipv6(string_id):
         try:
             return Ip.objects.get(address=string_id).interface
         except Ip.DoesNotExist as e:
             return None
     else:
         try:
             return Interface.objects.get(mac=string_id)
         except Interface.DoesNotExist as e:
             return None
Exemplo n.º 31
0
 def _get_default_dns_nameservers(network_details):
     ipv4_nameservers = []
     ipv6_nameservers = []
     for s in network_details.services:
         if isinstance(s, network_model.NameServerService):
             for nameserver in s.addresses:
                 if netaddr.valid_ipv6(nameserver):
                     ipv6_nameservers.append(nameserver)
                 else:
                     ipv4_nameservers.append(nameserver)
     return (ipv4_nameservers, ipv6_nameservers)
Exemplo n.º 32
0
def is_single_ipv6(ip):
    """
    to check a value if its IPv6 address

    Args:
        ip: the value to check if its IPv6

    Returns:
         True if it's IPv6 otherwise False
    """
    return netaddr.valid_ipv6(str(ip))
Exemplo n.º 33
0
 def addrstr(x):
     """
     helper for mapping IP addresses to zebra config statements
     """
     addr = x.split("/")[0]
     if netaddr.valid_ipv4(addr):
         return "ip address %s" % x
     elif netaddr.valid_ipv6(addr):
         return "ipv6 address %s" % x
     else:
         raise ValueError("invalid address: %s", x)
Exemplo n.º 34
0
def is_valid_ipv6(address):
    """Verify that address represents a valid IPv6 address.

    :param address: Value to verify
    :type address: string
    :returns: bool
    """
    try:
        return netaddr.valid_ipv6(address)
    except Exception:
        return False
Exemplo n.º 35
0
 def _get_link_interface(self, string_id):
     if valid_ipv4(string_id) or valid_ipv6(string_id):
         try:
             return Ip.objects.get(address=string_id).interface
         except Ip.DoesNotExist as e:
             return None
     else:
         try:
             return Interface.objects.get(mac=string_id)
         except Interface.DoesNotExist as e:
             return None
Exemplo n.º 36
0
 def addrstr(ip: netaddr.IPNetwork) -> str:
     """
     helper for mapping IP addresses to zebra config statements
     """
     address = str(ip.ip)
     if netaddr.valid_ipv4(address):
         return "ip address %s" % ip
     elif netaddr.valid_ipv6(address):
         return "ipv6 address %s" % ip
     else:
         raise ValueError("invalid address: %s", ip)
Exemplo n.º 37
0
def is_ip(ip):
    """Checks if an IP is a valid IPv4 or IPv6 address.

    Args:
        ip (str): The IP to be checked.

    Returns:
        True, if it's a valid IPv4 or IPv6 address
    """

    return valid_ipv4(ip) or valid_ipv6(ip)
Exemplo n.º 38
0
 def routestr(ip: netaddr.IPNetwork) -> str:
     address = str(ip.ip)
     if netaddr.valid_ipv6(address):
         dst = "3ffe:4::/64"
     else:
         dst = "10.9.8.0/24"
     if ip[-2] == ip[1]:
         return ""
     else:
         rtcmd = "#/sbin/ip route add %s via" % dst
         return "%s %s" % (rtcmd, ip[1])
Exemplo n.º 39
0
    def _output_hosts_file(self):
        """Writes a dnsmasq compatible dhcp hosts file.

        The generated file is sent to the --dhcp-hostsfile option of dnsmasq,
        and lists the hosts on the network which should receive a dhcp lease.
        Each line in this file is in the form::

            'mac_address,FQDN,ip_address'

        IMPORTANT NOTE: a dnsmasq instance does not resolve hosts defined in
        this file if it did not give a lease to a host listed in it (e.g.:
        multiple dnsmasq instances on the same network if this network is on
        multiple network nodes). This file is only defining hosts which
        should receive a dhcp lease, the hosts resolution in itself is
        defined by the `_output_addn_hosts_file` method.
        """
        buf = six.StringIO()
        filename = self.get_conf_file_name('host')

        LOG.debug('Building host file: %s', filename)
        dhcp_enabled_subnet_ids = [
            s.id for s in self.network.subnets if s.enable_dhcp
        ]
        # NOTE(ihrachyshka): the loop should not log anything inside it, to
        # avoid potential performance drop when lots of hosts are dumped
        for (port, alloc, hostname, name) in self._iter_hosts():
            if not alloc:
                if getattr(port, 'extra_dhcp_opts', False):
                    buf.write('%s,%s%s\n' %
                              (port.mac_address, 'set:', port.id))
                continue

            # don't write ip address which belongs to a dhcp disabled subnet.
            if alloc.subnet_id not in dhcp_enabled_subnet_ids:
                continue

            # (dzyu) Check if it is legal ipv6 address, if so, need wrap
            # it with '[]' to let dnsmasq to distinguish MAC address from
            # IPv6 address.
            ip_address = alloc.ip_address
            if netaddr.valid_ipv6(ip_address):
                ip_address = '[%s]' % ip_address

            if getattr(port, 'extra_dhcp_opts', False):
                buf.write(
                    '%s,%s,%s,%s%s\n' %
                    (port.mac_address, name, ip_address, 'set:', port.id))
            else:
                buf.write('%s,%s,%s\n' % (port.mac_address, name, ip_address))

        utils.replace_file(filename, buf.getvalue())
        LOG.debug('Done building host file %s with contents:\n%s', filename,
                  buf.getvalue())
        return filename
Exemplo n.º 40
0
def get_soap_url(protocol, host, path='sdk'):
    """Return URL to SOAP services for ESX/VC server.

    :param protocol: https or http
    :param host: ESX/VC server host IP
    :param path: path part of the SOAP URL
    :return: URL to SOAP services for ESX/VC server
    """
    if netaddr.valid_ipv6(host):
        return '%s://[%s]/%s' % (protocol, host, path)
    return '%s://%s/%s' % (protocol, host, path)
Exemplo n.º 41
0
def wbmanage(action, ruleset, address, direction='inbound', account='@.'):
# python /opt/iredapd/tools/wblist_admin.py --delete --blacklist 172.16.1.10
# --add --blacklist [email protected]
    array = address.split(' ')
    for i in range(len(array)):
        if netaddr.valid_ipv4(array[i]) or netaddr.valid_ipv6(array[i]):
            True
        elif '@' not in array[i]:
            array[i] = '@'+array[i]
    address = ' '.join(array)
    return __salt__['cmd.run']('python /opt/iredapd/tools/wblist_admin.py --'+direction+' --'+account+' --'+action+' --'+ruleset+' '+address)
Exemplo n.º 42
0
def is_valid_ip(address):
    if python_version(3, 3):
        import ipaddress
        try:
            ipaddress.ip_address(address)
            return True
        except ValueError:
            return False
    else:
        import netaddr
        return netaddr.valid_ipv4(address) or netaddr.valid_ipv6(address)
Exemplo n.º 43
0
    def _output_hosts_file(self):
        """Writes a dnsmasq compatible dhcp hosts file.

        The generated file is sent to the --dhcp-hostsfile option of dnsmasq,
        and lists the hosts on the network which should receive a dhcp lease.
        Each line in this file is in the form::

            'mac_address,FQDN,ip_address'

        IMPORTANT NOTE: a dnsmasq instance does not resolve hosts defined in
        this file if it did not give a lease to a host listed in it (e.g.:
        multiple dnsmasq instances on the same network if this network is on
        multiple network nodes). This file is only defining hosts which
        should receive a dhcp lease, the hosts resolution in itself is
        defined by the `_output_addn_hosts_file` method.
        """
        buf = six.StringIO()
        filename = self.get_conf_file_name('host')

        LOG.debug('Building host file: %s', filename)
        dhcp_enabled_subnet_ids = [s.id for s in self.network.subnets
                                   if s.enable_dhcp]
        # NOTE(ihrachyshka): the loop should not log anything inside it, to
        # avoid potential performance drop when lots of hosts are dumped
        for (port, alloc, hostname, name) in self._iter_hosts():
            if not alloc:
                if getattr(port, 'extra_dhcp_opts', False):
                    buf.write('%s,%s%s\n' %
                              (port.mac_address, 'set:', port.id))
                continue

            # don't write ip address which belongs to a dhcp disabled subnet.
            if alloc.subnet_id not in dhcp_enabled_subnet_ids:
                continue

            # (dzyu) Check if it is legal ipv6 address, if so, need wrap
            # it with '[]' to let dnsmasq to distinguish MAC address from
            # IPv6 address.
            ip_address = alloc.ip_address
            if netaddr.valid_ipv6(ip_address):
                ip_address = '[%s]' % ip_address

            if getattr(port, 'extra_dhcp_opts', False):
                buf.write('%s,%s,%s,%s%s\n' %
                          (port.mac_address, name, ip_address,
                           'set:', port.id))
            else:
                buf.write('%s,%s,%s\n' %
                          (port.mac_address, name, ip_address))

        utils.replace_file(filename, buf.getvalue())
        LOG.debug('Done building host file %s with contents:\n%s', filename,
                  buf.getvalue())
        return filename
Exemplo n.º 44
0
    def attach_container(self, container_name, br_name, veth_name, ip, tag=0, gw=False, txoff=False):
        logger.debug("Configure for container: {}".format(container_name))
        nspid = self.docker.nspid(container_name)

        self._create_veth(container_name, br_name, veth_name, tag)

        logger.debug("Container {}: add ip address {} for {}.\n".format(container_name, ip, veth_name))
        self._config_ip(nspid, veth_name, ip, txoff)

        if gw and netaddr.valid_ipv4(gw) or netaddr.valid_ipv6(gw):
            self._config_add_route(container_name, gw)
Exemplo n.º 45
0
    def _parse_subnets(self, subnets, link_name):
        networks = []

        if not subnets or not isinstance(subnets, list):
            LOG.warning("Subnets '%s' is empty or not a list.", subnets)
            return networks

        for subnet in subnets:
            if not isinstance(subnet, dict):
                LOG.warning("Subnet '%s' is not a dictionary", subnet)
                continue

            if subnet.get("type") in ["dhcp", "dhcp6"]:
                continue

            routes = []
            for route_data in subnet.get("routes", []):
                route_netmask = route_data.get("netmask")
                route_network = route_data.get("network")
                route_network_cidr = network_utils.ip_netmask_to_cidr(
                    route_network, route_netmask)

                route_gateway = route_data.get("gateway")
                route = network_model.Route(network_cidr=route_network_cidr,
                                            gateway=route_gateway)
                routes.append(route)

            address_cidr = subnet.get("address")
            netmask = subnet.get("netmask")
            if netmask:
                address_cidr = network_utils.ip_netmask_to_cidr(
                    address_cidr, netmask)

            gateway = subnet.get("gateway")
            if gateway:
                # Map the gateway as a default route, depending on the
                # IP family / version (4 or 6)
                gateway_net_cidr = "0.0.0.0/0"
                if netaddr.valid_ipv6(gateway):
                    gateway_net_cidr = "::/0"

                routes.append(
                    network_model.Route(network_cidr=gateway_net_cidr,
                                        gateway=gateway))

            networks.append(
                network_model.Network(
                    link=link_name,
                    address_cidr=address_cidr,
                    dns_nameservers=subnet.get("dns_nameservers"),
                    routes=routes))

        return networks
Exemplo n.º 46
0
def is_valid_ipv6(address):
    """ Helper to determine if we need the to add brackets.

    is_valid_ipv6('fd00::1')     -> True
    is_valid_ipv6('[fd00::1]')   -> False
    is_valid_ipv6('192.168.1.1') -> False

    """
    try:
        return netaddr.valid_ipv6(address)
    except Exception:
        return False
Exemplo n.º 47
0
def get_server_cls(host):
    """Return an appropriate WSGI server class base on provided host

    :param host: The listen host for the ceilometer API server.
    """
    server_cls = simple_server.WSGIServer
    if netaddr.valid_ipv6(host):
        # NOTE(dzyu) make sure use IPv6 sockets if host is in IPv6 pattern
        if getattr(server_cls, 'address_family') == socket.AF_INET:
            class server_cls(server_cls):
                address_family = socket.AF_INET6
    return server_cls
Exemplo n.º 48
0
 def data(self) -> Dict[str, Any]:
     routes = []
     for iface in self.node.get_ifaces(control=False):
         for ip in iface.ips():
             address = str(ip.ip)
             if netaddr.valid_ipv6(address):
                 dst = "3ffe:4::/64"
             else:
                 dst = "10.9.8.0/24"
             if ip[-2] != ip[1]:
                 routes.append((dst, ip[1]))
     return dict(routes=routes)
Exemplo n.º 49
0
 def __init__(self, Direccion):
     self.__id
     self.__direccion = netaddr.IPAddress(Direccion)
     if netaddr.valid_ipv4(Direccion):
         self.__familia = 4
     if netaddr.valid_ipv6(Direccion):
         self.__familia = 6
     self.__vrf
     self.__id_interfaz
     self.__fecha_creacion = datetime.datetime.now()
     self.__fecha_actualizacion
     self.__nat_interno
     self.__descripcion
Exemplo n.º 50
0
def get_server_cls(host):
    """Return an appropriate WSGI server class base on provided host

    :param host: The listen host for the entropy API server.
    """
    server_cls = simple_server.WSGIServer
    if netaddr.valid_ipv6(host):
        if getattr(server_cls, 'address_family') == socket.AF_INET:

            class server_cls(server_cls):
                address_family = socket.AF_INET6

    return server_cls
Exemplo n.º 51
0
 def get_link(cls, source, target, topology=None):
     """
     Find link between source and target, (or vice versa, order is irrelevant).
     :param source: ip or mac addresses
     :param target: ip or mac addresses
     :param topology: optional topology relation
     :returns: Link object
     :raises: LinkNotFound
     """
     a = source
     b = target
     # ensure parameters are coherent
     if not (valid_ipv4(a) and valid_ipv4(b)) and not (valid_ipv6(a) and valid_ipv6(b)) and not (valid_mac(a) and valid_mac(b)):
         raise ValueError('Expecting valid ipv4, ipv6 or mac address')
     # get interfaces
     a = cls._get_link_interface(a)
     b = cls._get_link_interface(b)
     # raise LinkDataNotFound if an interface is not found
     not_found = []
     if a is None:
         not_found.append(source)
     if b is None:
         not_found.append(target)
     if not_found:
         msg = 'the following interfaces could not be found: {0}'.format(', '.join(not_found))
         raise LinkDataNotFound(msg)
     # find link with interfaces
     # inverse order is also ok
     q = (Q(interface_a=a, interface_b=b) | Q(interface_a=b, interface_b=a))
     # add topology to lookup
     if topology:
         q = q & Q(topology=topology)
     link = Link.objects.filter(q).first()
     if link is None:
         raise LinkNotFound('Link matching query does not exist',
                            interface_a=a,
                            interface_b=b,
                            topology=topology)
     return link
Exemplo n.º 52
0
def netlist(nets):
    v4nets = []
    v6nets = []
    for net in nets:
        ipNetwork = netaddr.IPNetwork(net)
        parts = str(ipNetwork).split("/")
        ip = parts[0]
        mask = parts[1]
        if netaddr.valid_ipv4(ip) and int(mask) <= 32:
            v4nets.append(ipNetwork)
        elif netaddr.valid_ipv6(ip) and int(mask) <= 128:
            v6nets.append(ipNetwork)
    return v4nets, v6nets
Exemplo n.º 53
0
def isIP6(IP):
    """
    to check a value if its IPv6 address

    Args:
        IP: the value to check if its IPv6

    Returns:
         True if it's IPv6 otherwise False
    """
    IP = str(IP)
    ip_flag = netaddr.valid_ipv6(IP)
    return ip_flag
Exemplo n.º 54
0
def is_valid_ipv6(address):
    """Verify that address represents a valid IPv6 address.

    :param address: Value to verify
    :type address: string
    :returns: bool

    .. versionadded:: 1.1
    """
    try:
        return netaddr.valid_ipv6(address)
    except netaddr.AddrFormatError:
        return False
Exemplo n.º 55
0
 def _check_rf_and_normalize(prefix):
     """ check prefix's route_family and if the address is
     IPv6 address, return IPv6 route_family and normalized IPv6 address.
     If the address is IPv4 address, return IPv4 route_family
     and the prefix itself.
     """
     ip, masklen = prefix.split('/')
     if netaddr.valid_ipv6(ip):
         # normalize IPv6 address
         ipv6_prefix = str(netaddr.IPAddress(ip)) + '/' + masklen
         return vrfs.VRF_RF_IPV6, ipv6_prefix
     else:
         return vrfs.VRF_RF_IPV4, prefix
Exemplo n.º 56
0
    def serialize(self):
        # fixup
        if (netaddr.valid_ipv4(self.peer_ip)
                and netaddr.valid_ipv4(self.local_ip)):
            self.afi = self.AFI_IPv4
        elif (netaddr.valid_ipv6(self.peer_ip)
              and netaddr.valid_ipv6(self.local_ip)):
            self.afi = self.AFI_IPv6
        else:
            raise ValueError(
                'peer_ip and local_ip must be the same address family: '
                'peer_ip=%s, local_ip=%s' % (self.peer_ip, self.local_ip))

        buf = struct.pack(self._HEADER_FMT,
                          self.peer_as, self.local_as,
                          self.if_index, self.afi)

        buf += ip.text_to_bin(self.peer_ip)
        buf += ip.text_to_bin(self.local_ip)

        buf += self.bgp_message.serialize()

        return buf
Exemplo n.º 57
0
def validate_ip(ip_addr, version):
    """
    Validate that ip_addr is a valid IPv4 or IPv6 address

    :param ip_addr: IP address to be validated
    :param version: 4 or 6
    :return: Boolean: True if valid, False if invalid.
    """
    assert version in (4, 6)

    if version == 4:
        return netaddr.valid_ipv4(ip_addr)
    if version == 6:
        return netaddr.valid_ipv6(ip_addr)
Exemplo n.º 58
0
def get_transport_function(ip, port):
    if '%' in ip:
        ip, interface = ip.split('%', 1)
        if not re.match('[a-z][a-z0-9]+$', interface):
            logging.warn("Invalid IP Address")
            return None
        else:
            return Udp6TransportTarget((ip, port))

    elif netaddr.valid_ipv4(ip):
        return cmdgen.UdpTransportTarget((ip, port))

    elif netaddr.valid_ipv6(ip):
        return Udp6TransportTarget((ip, port))
Exemplo n.º 59
0
    def find_default_gwv6(self):
        """
        Find host default ipv6 gateway

        Returns:
            str: Default gateway
        """
        out = self._cmd(["ip", "-6", "route"]).splitlines()
        for i in out:
            if re.search("default", i):
                default_gw = re.findall(r'(?<=\s)[0-9a-fA-F:]{3,}(?=\s)', i)
                if netaddr.valid_ipv6(default_gw[0]):
                    return default_gw[0]
        return None
Exemplo n.º 60
0
 def data(self) -> Dict[str, Any]:
     interfaces = []
     for ifc in self.node.netifs():
         if getattr(ifc, "control", False):
             continue
         prefixes = []
         for x in ifc.addrlist:
             addr = x.split("/")[0]
             if netaddr.valid_ipv6(addr):
                 prefixes.append(x)
         if not prefixes:
             continue
         interfaces.append((ifc.name, prefixes))
     return dict(interfaces=interfaces)