def connect(self, keyfile=None, timeout=5):
        """
        Connect to the node via SSH.

        :param keyfile: Path to the SSH host key.
        :param timeout: Maximum time to wait (in seconds) for the TCP
            connection to be established.

        :return: :py:class:`paramiko.SSHClient` - ssh connection or None on
                 failure
        """
        ssh = paramiko.SSHClient()
        ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        if keyfile and os.path.exists(keyfile):
            ssh.load_host_keys(keyfile)

        # Try connecting using the `preferred_ip`, if
        # present. Otherwise, try all of them and set `preferred_ip`
        # using the first that is working.
        ips = self.ips[:]
        # This is done in order to "sort" the IPs and put the preferred_ip first.
        if self.preferred_ip:
            if self.preferred_ip in ips:
                ips.remove(self.preferred_ip)
            else:
                # Preferred is changed?
                log.debug(
                    "IP address %s does not seem to belong to %s anymore."
                    " Ignoring it.", self.preferred_ip, self.name)
                self.preferred_ip = ips[0]

        for ip in itertools.chain([self.preferred_ip], ips):
            if not ip:
                continue
            log.debug(
                "Trying to connect to host %s using IP address %s ...",
                self.name, ip)
            try:
                addr, port = parse_ip_address_and_port(ip, SSH_PORT)
                extra = {
                    'allow_agent':   True,
                    'key_filename':  self.user_key_private,
                    'look_for_keys': False,
                    'timeout':       timeout,
                    'username':      self.image_user,
                }
                if self.ssh_proxy_command:
                    proxy_command = expand_ssh_proxy_command(
                        self.ssh_proxy_command,
                        self.image_user, addr, port)
                    from paramiko.proxy import ProxyCommand
                    extra['sock'] = ProxyCommand(proxy_command)
                    log.debug("Using proxy command `%s`.", proxy_command)
                ssh.connect(str(addr), port=port, **extra)
                log.debug(
                    "Connection to %s succeeded on port %d,"
                    " will use this IP address for future connections.",
                    ip, port)
                if ip != self.preferred_ip:
                    self.preferred_ip = ip
                # Connection successful.
                return ssh
            except socket.error as ex:
                log.debug(
                    "Host %s (%s) not reachable within %d seconds: %s -- %r",
                    self.name, ip, timeout, ex, type(ex))
            except paramiko.BadHostKeyException as ex:
                log.error(
                    "Invalid SSH host key for %s (%s): %s.",
                    self.name, ip, ex)
            except paramiko.SSHException as ex:
                log.debug(
                    "Ignoring error connecting to %s: %s -- %r",
                    self.name, ex, type(ex))

        return None
Connect to the node via SSH.

        :param keyfile: Path to the SSH host key.
        :param timeout: Maximum time to wait (in seconds) for the TCP
            connection to be established.

        :return: :py:class:`paramiko.SSHClient` - ssh connection or None on
                 failure