Beispiel #1
0
    def _get_ssh_connection(self):
        """Returns an ssh connection to the specified host."""
        _timeout = True
        ssh = paramiko.SSHClient()
        ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        _start_time = time.time()

        while not self._is_timed_out(self.timeout, _start_time):
            try:
                ssh.connect(self.host,
                            username=self.username,
                            password=self.password,
                            look_for_keys=self.look_for_keys,
                            key_filename=self.key_filename,
                            timeout=self.timeout,
                            pkey=self.pkey)
                _timeout = False
                break
            except socket.error:
                continue
            except paramiko.AuthenticationException:
                time.sleep(5)
                continue
        if _timeout:
            raise exceptions.SSHTimeout(host=self.host,
                                        user=self.username,
                                        password=self.password)
        return ssh
Beispiel #2
0
 def get_ssh_connection(self, host, username, password):
     """Create an SSH connection object to a host"""
     ssh_timeout = self.config.compute.ssh_timeout
     ssh_client = Client(host, username, password, ssh_timeout)
     if not ssh_client.test_connection_auth():
         raise exceptions.SSHTimeout()
     else:
         return ssh_client
Beispiel #3
0
 def _get_ssh_connection(self, sleep=1.5, backoff=1):
     """Returns an ssh connection to the specified host."""
     bsleep = sleep
     ssh = paramiko.SSHClient()
     ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
     _start_time = time.time()
     if self.pkey is not None:
         LOG.info(
             "Creating ssh connection to '%s' as '%s'"
             " with public key authentication", self.host, self.username)
     else:
         LOG.info(
             "Creating ssh connection to '%s' as '%s'"
             " with password %s", self.host, self.username,
             str(self.password))
     attempts = 0
     while True:
         try:
             ssh.connect(self.host,
                         username=self.username,
                         password=self.password,
                         look_for_keys=self.look_for_keys,
                         key_filename=self.key_filename,
                         timeout=self.channel_timeout,
                         pkey=self.pkey)
             LOG.info("ssh connection to %s@%s successfuly created",
                      self.username, self.host)
             return ssh
         except (socket.error, paramiko.SSHException) as e:
             if self._is_timed_out(_start_time):
                 LOG.exception(
                     "Failed to establish authenticated ssh"
                     " connection to %s@%s after %d attempts",
                     self.username, self.host, attempts)
                 raise exceptions.SSHTimeout(host=self.host,
                                             user=self.username,
                                             password=self.password)
             bsleep += backoff
             attempts += 1
             LOG.warning(
                 "Failed to establish authenticated ssh"
                 " connection to %s@%s (%s). Number attempts: %s."
                 " Retry after %d seconds.", self.username, self.host, e,
                 attempts, bsleep)
             time.sleep(bsleep)