Example #1
0
    def _run_ssh(self, cmd_list, check_exit=True, attempts=2):
        self.check_ssh_injection(cmd_list)
        command = ' '.join(cmd_list)

        try:
            total_attempts = attempts
            while attempts > 0:
                attempts -= 1
                try:
                    return self._ssh_execute(command,
                                             check_exit_code=check_exit)
                except Exception as e:
                    self._logger.error(e)
                    if attempts > 0:
                        greenthread.sleep(randint(20, 500) / 100.0)
                    if not self.ssh.get_transport().is_alive():
                        self._create_ssh()

            msg = ("SSH Command failed after '%(total_attempts)r' "
                   "attempts : '%(command)s'" % {
                       'total_attempts': total_attempts,
                       'command': command
                   })
            self._logger.error(msg)
            raise exceptions.SSHException(message=msg)
        except Exception:
            self._logger.error("Error running ssh command: %s" % command)
            raise
Example #2
0
 def _connect(self, ssh):
     if self.san_password:
         ssh.connect(self.san_ip,
                     port=self.san_ssh_port,
                     username=self.san_login,
                     password=self.san_password,
                     timeout=self.ssh_conn_timeout)
     elif self.san_privatekey:
         pkfile = os.path.expanduser(self.san_privatekey)
         privatekey = paramiko.RSAKey.from_private_key_file(pkfile)
         ssh.connect(self.san_ip,
                     port=self.san_ssh_port,
                     username=self.san_login,
                     pkey=privatekey,
                     timeout=self.ssh_conn_timeout)
     else:
         msg = "Specify a password or private_key"
         raise exceptions.SSHException(msg)
Example #3
0
    def _create_ssh(self, **kwargs):
        try:
            ssh = paramiko.SSHClient()

            known_hosts_file = kwargs.get('known_hosts_file', None)
            if known_hosts_file is None:
                ssh.load_system_host_keys()
            else:
                # Make sure we can open the file for appending first.
                # This is needed to create the file when we run CI tests with
                # no existing key file.
                open(known_hosts_file, 'a').close()
                ssh.load_host_keys(known_hosts_file)

            missing_key_policy = kwargs.get('missing_key_policy', None)
            if missing_key_policy is None:
                missing_key_policy = paramiko.AutoAddPolicy()
            elif isinstance(missing_key_policy, basestring):
                # To make it configurable, allow string to be mapped to object.
                if missing_key_policy == paramiko.AutoAddPolicy().__class__.\
                        __name__:
                    missing_key_policy = paramiko.AutoAddPolicy()
                elif missing_key_policy == paramiko.RejectPolicy().__class__.\
                        __name__:
                    missing_key_policy = paramiko.RejectPolicy()
                elif missing_key_policy == paramiko.WarningPolicy().__class__.\
                        __name__:
                    missing_key_policy = paramiko.WarningPolicy()
                else:
                    raise exceptions.SSHException(
                        "Invalid missing_key_policy: %s" % missing_key_policy
                    )

            ssh.set_missing_host_key_policy(missing_key_policy)

            self.ssh = ssh
        except Exception as e:
            msg = "Error connecting via ssh: %s" % e
            self._logger.error(msg)
            raise paramiko.SSHException(msg)
Example #4
0
 def raise_stripper_error(reason, output):
     msg = "Multi-line stripper failed: %s" % reason
     HPE3PARSSHClient._logger.error(msg)
     HPE3PARSSHClient._logger.debug("Output: %s" %
                                    HPE3PARSSHClient.sanitize_cert(output))
     raise exceptions.SSHException(msg)