def get_ssh(self, host):
        """
        Ssh connection. The actual connection to the host is established
        only after the first call of this function.
        """
        # Lock: be sure not to create this connection from several threads at
        # the same time.
        with self._lock:
            if not (self._ssh_cache and self._ssh_cache._transport and self._ssh_cache._transport.is_active()):
                # Create progress bar.
                progress_bar = self._create_connect_progress_bar(host)

                with progress_bar:
                    h = host

                    # Connect
                    self._ssh_cache = paramiko.SSHClient()

                    if not h.reject_unknown_hosts:
                        self._ssh_cache.set_missing_host_key_policy(paramiko.AutoAddPolicy())

                    try:
                        kw = {}

                        if h.config_filename:
                            try:
                                config_file = file(os.path.expanduser(h.config_filename))
                            except IOError:
                                pass
                            else:
                                ssh_config = paramiko.config.SSHConfig()
                                ssh_config.parse(config_file)
                                host_config = ssh_config.lookup(h.address)

                                # Map ssh_config to paramiko config
                                config_map = {
                                        'identityfile': 'key_filename',
                                        'user': 'username',
                                        'port': 'port',
                                        'connecttimeout': 'timeout',
                                        }
                                for ck, pk in config_map.items():
                                    if ck in host_config:
                                        kw[pk] = host_config[ck]

                        if h.port:
                            kw['port'] = h.port
                        if h.username:
                            kw['username'] = h.username
                        if h.timeout:
                            kw['timeout'] = h.timeout

                        # Paramiko's authentication method can be either a public key, public key file, or password.
                        if h.rsa_key:
                            # RSA key
                            rsa_key_file_obj = StringIO.StringIO(h.rsa_key)
                            kw["pkey"] = paramiko.RSAKey.from_private_key(rsa_key_file_obj, h.rsa_key_password)
                        elif h.key_filename:
                            kw["key_filename"] = h.key_filename
                        elif h.password:
                            kw["password"] = h.password

                        # Connect to the SSH server.
                        # We use a patched connect function instead of the connect of paramiko's library,
                        # In order to add the progress bar.
                        from .paramiko_connect_patch import connect as connect_patch
                        kw['progress_bar_callback'] = progress_bar.set_progress

                        #self._ssh_cache.connect = connect_patch
                        connect_patch(self._ssh_cache, h.address, **kw)

                    except (paramiko.SSHException, Exception) as e:
                        self._ssh_cache = None
                        raise ConnectionFailedException('Could not connect to host %s:%s (%s) username=%s\n%s' %
                                        (h.address, h.port, h.slug, h.username, unicode(e)))

            return self._ssh_cache
dataset/ETHPy150Open jonathanslenders/python-deployer/deployer/host/ssh.py/SSHBackend.get_ssh/original