Пример #1
0
 def test_check_ssh_injection(self):
     cmd_list = ['ssh', '-D', 'my_name@name_of_remote_computer']
     self.assertIsNone(utils.check_ssh_injection(cmd_list))
     cmd_list = ['echo', '"quoted arg with space"']
     self.assertIsNone(utils.check_ssh_injection(cmd_list))
     cmd_list = ['echo', "'quoted arg with space'"]
     self.assertIsNone(utils.check_ssh_injection(cmd_list))
Пример #2
0
    def _run_ssh(self, cmd_list, check_exit=True, attempts=1):
        utils.check_ssh_injection(cmd_list)
        command = ' '. join(cmd_list)

        if not self.sshpool:
            self.sshpool = utils.SSHPool(self.config.san_ip,
                                         self.config.san_ssh_port,
                                         self.config.ssh_conn_timeout,
                                         self.config.san_login,
                                         password=self.config.san_password,
                                         privatekey=
                                         self.config.san_private_key,
                                         min_size=
                                         self.config.ssh_min_pool_conn,
                                         max_size=
                                         self.config.ssh_max_pool_conn)
        try:
            total_attempts = attempts
            with self.sshpool.item() as ssh:
                while attempts > 0:
                    attempts -= 1
                    try:
                        return self._ssh_execute(ssh, command,
                                                 check_exit_code=check_exit)
                    except Exception as e:
                        LOG.error(e)
                        greenthread.sleep(randint(20, 500) / 100.0)
                msg = (_("SSH Command failed after '%(total_attempts)r' "
                         "attempts : '%(command)s'") %
                       {'total_attempts': total_attempts, 'command': command})
                raise paramiko.SSHException(msg)
        except Exception:
            with excutils.save_and_reraise_exception():
                LOG.error(_("Error running ssh command: %s") % command)
Пример #3
0
 def test_check_ssh_injection(self):
     cmd_list = ['ssh', '-D', 'my_name@name_of_remote_computer']
     self.assertIsNone(utils.check_ssh_injection(cmd_list))
     cmd_list = ['echo', '"quoted arg with space"']
     self.assertIsNone(utils.check_ssh_injection(cmd_list))
     cmd_list = ['echo', "'quoted arg with space'"]
     self.assertIsNone(utils.check_ssh_injection(cmd_list))
Пример #4
0
 def test_check_ssh_injection(self):
     cmd_list = ["ssh", "-D", "my_name@name_of_remote_computer"]
     self.assertIsNone(utils.check_ssh_injection(cmd_list))
     cmd_list = ["echo", '"quoted arg with space"']
     self.assertIsNone(utils.check_ssh_injection(cmd_list))
     cmd_list = ["echo", "'quoted arg with space'"]
     self.assertIsNone(utils.check_ssh_injection(cmd_list))
Пример #5
0
    def _run_ssh(self, cmd_list, check_exit=True, attempts=1):
        utils.check_ssh_injection(cmd_list)
        command = ' '.join(cmd_list)

        if not self.sshpool:
            self.sshpool = utils.SSHPool(
                self.config.san_ip,
                self.config.san_ssh_port,
                self.config.ssh_conn_timeout,
                self.config.san_login,
                password=self.config.san_password,
                privatekey=self.config.san_private_key,
                min_size=self.config.ssh_min_pool_conn,
                max_size=self.config.ssh_max_pool_conn)
        try:
            total_attempts = attempts
            with self.sshpool.item() as ssh:
                while attempts > 0:
                    attempts -= 1
                    try:
                        return self._ssh_execute(ssh,
                                                 command,
                                                 check_exit_code=check_exit)
                    except Exception as e:
                        LOG.error(e)
                        greenthread.sleep(randint(20, 500) / 100.0)
                msg = (_("SSH Command failed after '%(total_attempts)r' "
                         "attempts : '%(command)s'") % {
                             'total_attempts': total_attempts,
                             'command': command
                         })
                raise paramiko.SSHException(msg)
        except Exception:
            with excutils.save_and_reraise_exception():
                LOG.error(_("Error running ssh command: %s") % command)
    def _execute_shell_cmd(self, cmd):
        """Run command over shell for older firmware versions.

        Invokes shell and issue the command and return the output.
        This is primarily used for issuing read commands when we are not sure
        if the firmware supports exec_command.
        """
        utils.check_ssh_injection(cmd)
        command = " ".join(cmd)
        stdout, stderr = None, None
        if not self.sshpool:
            self.sshpool = ssh_utils.SSHPool(
                self.switch_ip,
                self.switch_port,
                None,
                self.switch_user,
                self.switch_pwd,
                self.switch_key,
                min_size=1,
                max_size=5,
            )
        with self.sshpool.item() as ssh:
            LOG.debug("Running cmd (SSH): %s", command)
            channel = ssh.invoke_shell()
            stdin_stream = channel.makefile("wb")
            stdout_stream = channel.makefile("rb")
            stderr_stream = channel.makefile("rb")
            stdin_stream.write(
                """%s
exit
"""
                % command
            )
            stdin_stream.flush()
            stdout = stdout_stream.readlines()
            stderr = stderr_stream.readlines()
            stdin_stream.close()
            stdout_stream.close()
            stderr_stream.close()

            exit_status = channel.recv_exit_status()
            # exit_status == -1 if no exit code was returned
            if exit_status != -1:
                LOG.debug("Result was %s", exit_status)
                if exit_status != 0:
                    LOG.debug("command %s failed", command)
                    raise processutils.ProcessExecutionError(
                        exit_code=exit_status, stdout=stdout, stderr=stderr, cmd=command
                    )
            try:
                channel.close()
            except Exception:
                LOG.exception(_LE("Error closing channel."))
            LOG.debug("_execute_cmd: stdout to return: %s", stdout)
            LOG.debug("_execute_cmd: stderr to return: %s", stderr)
        return (stdout, stderr)
Пример #7
0
 def _run_ssh(self, cmd_list, check_exit_code=True):
     cinder_utils.check_ssh_injection(cmd_list)
     command = ' '.join(cmd_list)
     if not self.sshpool:
         try:
             self.sshpool = self._set_up_sshpool(self.configuration.san_ip)
         except paramiko.SSHException as e:
             raise exception.VolumeDriverException(message=e)
     ssh_execute = self._ssh_execute(self.sshpool, command, check_exit_code)
     return ssh_execute
Пример #8
0
    def run_cmd(self, cmd, ip0, user, pw, *args, **kwargs):
        """Run a command on SMU or using SSH

        :param cmd: the command that will be run on SMU
        :param ip0: string IP address of controller
        :param user: string user authentication for array
        :param pw: string password authentication for array
        :returns: formated string with version information
        """
        LOG.debug('Enable ssh: %s',
                  six.text_type(self.drv_configs['ssh_enabled']))

        if self.drv_configs['ssh_enabled'] != 'True':
            # Direct connection via ssc
            args = (cmd, '-u', user, '-p', pw, ip0) + args
            out, err = utils.execute(*args, **kwargs)
            LOG.debug("command %(cmd)s result: out = %(out)s - err = "
                      "%(err)s", {'cmd': cmd, 'out': out, 'err': err})
            return out, err
        else:
            if self.drv_configs['cluster_admin_ip0'] is None:
                # Connect to SMU through SSH and run ssc locally
                args = (cmd, 'localhost') + args
            else:
                args = (cmd, '--smuauth',
                        self.drv_configs['cluster_admin_ip0']) + args

            utils.check_ssh_injection(args)
            command = ' '.join(args)
            command = command.replace('"', '\\"')

            if not self.sshpool:
                server = self.drv_configs['mgmt_ip0']
                port = int(self.drv_configs['ssh_port'])
                username = self.drv_configs['username']
                # We only accept private/public key auth
                password = ""
                privatekey = self.drv_configs['ssh_private_key']
                self.sshpool = ssh_utils.SSHPool(server,
                                                 port,
                                                 None,
                                                 username,
                                                 password=password,
                                                 privatekey=privatekey)

            with self.sshpool.item() as ssh:
                try:
                    out, err = processutils.ssh_execute(ssh, command,
                                                        check_exit_code=True)
                    LOG.debug("command %(cmd)s result: out = %(out)s - err = "
                              "%(err)s", {'cmd': cmd, 'out': out, 'err': err})
                    return out, err
                except processutils.ProcessExecutionError:
                    LOG.error(_LE("Error running SSH command."))
                    raise
Пример #9
0
    def run_cmd(self, cmd, ip0, user, pw, *args, **kwargs):
        """Run a command on SMU or using SSH

        :param cmd: the command that will be run on SMU
        :param ip0: string IP address of controller
        :param user: string user authentication for array
        :param pw: string password authentication for array
        :returns: formated string with version information
        """
        LOG.debug('Enable ssh: %s',
                  six.text_type(self.drv_configs['ssh_enabled']))

        if self.drv_configs['ssh_enabled'] != 'True':
            # Direct connection via ssc
            args = (cmd, '-u', user, '-p', pw, ip0) + args
            out, err = utils.execute(*args, **kwargs)
            LOG.debug("command %(cmd)s result: out = %(out)s - err = "
                      "%(err)s", {'cmd': cmd, 'out': out, 'err': err})
            return out, err
        else:
            if self.drv_configs['cluster_admin_ip0'] is None:
                # Connect to SMU through SSH and run ssc locally
                args = (cmd, 'localhost') + args
            else:
                args = (cmd, '--smuauth',
                        self.drv_configs['cluster_admin_ip0']) + args

            utils.check_ssh_injection(args)
            command = ' '.join(args)
            command = command.replace('"', '\\"')

            if not self.sshpool:
                server = self.drv_configs['mgmt_ip0']
                port = int(self.drv_configs['ssh_port'])
                username = self.drv_configs['username']
                # We only accept private/public key auth
                password = ""
                privatekey = self.drv_configs['ssh_private_key']
                self.sshpool = ssh_utils.SSHPool(server,
                                                 port,
                                                 None,
                                                 username,
                                                 password=password,
                                                 privatekey=privatekey)

            with self.sshpool.item() as ssh:
                try:
                    out, err = processutils.ssh_execute(ssh, command,
                                                        check_exit_code=True)
                    LOG.debug("command %(cmd)s result: out = %(out)s - err = "
                              "%(err)s", {'cmd': cmd, 'out': out, 'err': err})
                    return out, err
                except processutils.ProcessExecutionError:
                    LOG.error(_LE("Error running SSH command."))
                    raise
Пример #10
0
    def _execute_shell_cmd(self, cmd):
        """Run command over shell for older firmware versions.

        Invokes shell and issue the command and return the output.
        This is primarily used for issuing read commands when we are not sure
        if the firmware supports exec_command.
        """
        utils.check_ssh_injection(cmd)
        command = ' '.join(cmd)
        stdout, stderr = None, None
        if not self.sshpool:
            self.sshpool = ssh_utils.SSHPool(self.switch_ip,
                                             self.switch_port,
                                             None,
                                             self.switch_user,
                                             self.switch_pwd,
                                             self.switch_key,
                                             min_size=1,
                                             max_size=5)
        with self.sshpool.item() as ssh:
            LOG.debug('Running cmd (SSH): %s', command)
            channel = ssh.invoke_shell()
            stdin_stream = channel.makefile('wb')
            stdout_stream = channel.makefile('rb')
            stderr_stream = channel.makefile('rb')
            stdin_stream.write('''%s
exit
''' % command)
            stdin_stream.flush()
            stdout = stdout_stream.readlines()
            stderr = stderr_stream.readlines()
            stdin_stream.close()
            stdout_stream.close()
            stderr_stream.close()

            exit_status = channel.recv_exit_status()
            # exit_status == -1 if no exit code was returned
            if exit_status != -1:
                LOG.debug('Result was %s', exit_status)
                if exit_status != 0:
                    LOG.debug("command %s failed", command)
                    raise processutils.ProcessExecutionError(
                        exit_code=exit_status,
                        stdout=stdout,
                        stderr=stderr,
                        cmd=command)
            try:
                channel.close()
            except Exception:
                LOG.exception('Error closing channel.')
            LOG.debug("_execute_cmd: stdout to return: %s", stdout)
            LOG.debug("_execute_cmd: stderr to return: %s", stderr)
        return (stdout, stderr)
Пример #11
0
    def _get_switch_data(self, ssh_pool, cmd):
        utils.check_ssh_injection([cmd])

        with ssh_pool.item() as ssh:
            try:
                switch_data, err = processutils.ssh_execute(ssh, cmd)
            except processutils.ProcessExecutionError as e:
                msg = (_("SSH Command failed with error: '%(err)s', Command: "
                         "'%(command)s'") % {'err': six.text_type(e),
                                             'command': cmd})
                LOG.error(msg)
                raise exception.FCSanLookupServiceException(message=msg)

        return switch_data
Пример #12
0
    def _ssh(self, cmd, check_exit_code=True):
        try:
            LOG.debug('Run CLI command: %s' % cmd)
            utils.check_ssh_injection(cmd)
            ret = self.fake_storage.execute_command(cmd, check_exit_code)
            (stdout, stderr) = ret
            LOG.debug('CLI output:\n stdout: %(stdout)s\n stderr: '
                      '%(stderr)s' % {'stdout': stdout, 'stderr': stderr})

        except processutils.ProcessExecutionError as e:
            with excutils.save_and_reraise_exception():
                LOG.debug('CLI Exception output:\n stdout: %(out)s\n '
                          'stderr: %(err)s' % {'out': e.stdout,
                                               'err': e.stderr})
        return ret
Пример #13
0
    def _ssh(self, cmd, check_exit_code=True):
        try:
            LOG.debug('Run CLI command: %s' % cmd)
            utils.check_ssh_injection(cmd)
            ret = self.fake_storage.execute_command(cmd, check_exit_code)
            (stdout, stderr) = ret
            LOG.debug('CLI output:\n stdout: %(stdout)s\n stderr: '
                      '%(stderr)s' % {'stdout': stdout, 'stderr': stderr})

        except processutils.ProcessExecutionError as e:
            with excutils.save_and_reraise_exception():
                LOG.debug('CLI Exception output:\n stdout: %(out)s\n '
                          'stderr: %(err)s' % {'out': e.stdout,
                                               'err': e.stderr})
        return ret
Пример #14
0
    def _run_ssh(self, cmd_list, check_exit_code=True, attempts=1):
        utils.check_ssh_injection(cmd_list)
        command = ' '. join(cmd_list)

        if not self.sshpool:
            password = self.configuration.san_password
            privatekey = self.configuration.san_private_key
            min_size = self.configuration.ssh_min_pool_conn
            max_size = self.configuration.ssh_max_pool_conn
            self.sshpool = ssh_utils.SSHPool(
                self.configuration.san_ip,
                self.configuration.san_ssh_port,
                self.configuration.ssh_conn_timeout,
                self.configuration.san_login,
                password=password,
                privatekey=privatekey,
                min_size=min_size,
                max_size=max_size)
        last_exception = None
        try:
            with self.sshpool.item() as ssh:
                while attempts > 0:
                    attempts -= 1
                    try:
                        return processutils.ssh_execute(
                            ssh,
                            command,
                            check_exit_code=check_exit_code)
                    except Exception as e:
                        LOG.error(e)
                        last_exception = e
                        greenthread.sleep(random.randint(20, 500) / 100.0)
                try:
                    raise processutils.ProcessExecutionError(
                        exit_code=last_exception.exit_code,
                        stdout=last_exception.stdout,
                        stderr=last_exception.stderr,
                        cmd=last_exception.cmd)
                except AttributeError:
                    raise processutils.ProcessExecutionError(
                        exit_code=-1,
                        stdout="",
                        stderr="Error running SSH command",
                        cmd=command)

        except Exception:
            with excutils.save_and_reraise_exception():
                LOG.error("Error running SSH command: %s", command)
Пример #15
0
    def _run_ssh(self, cmd_list, check_exit_code=True, attempts=1):
        utils.check_ssh_injection(cmd_list)
        command = ' '. join(cmd_list)

        if not self.sshpool:
            password = self.configuration.san_password
            privatekey = self.configuration.san_private_key
            min_size = self.configuration.ssh_min_pool_conn
            max_size = self.configuration.ssh_max_pool_conn
            self.sshpool = ssh_utils.SSHPool(
                self.configuration.san_ip,
                self.configuration.san_ssh_port,
                self.configuration.ssh_conn_timeout,
                self.configuration.san_login,
                password=password,
                privatekey=privatekey,
                min_size=min_size,
                max_size=max_size)
        last_exception = None
        try:
            with self.sshpool.item() as ssh:
                while attempts > 0:
                    attempts -= 1
                    try:
                        return processutils.ssh_execute(
                            ssh,
                            command,
                            check_exit_code=check_exit_code)
                    except Exception as e:
                        LOG.error(e)
                        last_exception = e
                        greenthread.sleep(random.randint(20, 500) / 100.0)
                try:
                    raise processutils.ProcessExecutionError(
                        exit_code=last_exception.exit_code,
                        stdout=last_exception.stdout,
                        stderr=last_exception.stderr,
                        cmd=last_exception.cmd)
                except AttributeError:
                    raise processutils.ProcessExecutionError(
                        exit_code=-1,
                        stdout="",
                        stderr="Error running SSH command",
                        cmd=command)

        except Exception:
            with excutils.save_and_reraise_exception():
                LOG.error("Error running SSH command: %s", command)
    def _get_switch_data(self, ssh_pool, cmd):
        utils.check_ssh_injection([cmd])

        with ssh_pool.item() as ssh:
            try:
                switch_data, err = processutils.ssh_execute(ssh, cmd)
            except processutils.ProcessExecutionError as e:
                msg = (_("SSH Command failed with error: '%(err)s', Command: "
                         "'%(command)s'") % {
                             'err': six.text_type(e),
                             'command': cmd
                         })
                LOG.error(msg)
                raise exception.FCSanLookupServiceException(message=msg)

        return switch_data
Пример #17
0
    def _run_cmd(self, *args, **kwargs):
        """Runs a command on SMU using SSH.

        :returns: stdout and stderr of the command
        """
        if self.cluster_admin_ip0 is None:
            # Connect to SMU through SSH and run ssc locally
            args = (self.hnas_cmd, 'localhost') + args
        else:
            args = (self.hnas_cmd, '--smuauth', self.cluster_admin_ip0) + args

        utils.check_ssh_injection(args)
        command = ' '.join(args)
        command = command.replace('"', '\\"')

        if not self.sshpool:
            self.sshpool = ssh_utils.SSHPool(ip=self.mgmt_ip0,
                                             port=int(self.ssh_port),
                                             conn_timeout=None,
                                             login=self.ssh_username,
                                             password=self.ssh_pwd,
                                             privatekey=self.ssh_private_key)

        with self.sshpool.item() as ssh:
            try:
                out, err = putils.ssh_execute(ssh,
                                              command,
                                              check_exit_code=True)
                LOG.debug(
                    "command %(cmd)s result: out = "
                    "%(out)s - err = %(err)s", {
                        'cmd': self.hnas_cmd,
                        'out': out,
                        'err': err
                    })
                return out, err
            except putils.ProcessExecutionError as e:
                if 'Failed to establish SSC connection' in e.stderr:
                    msg = _("Failed to establish SSC connection!")
                    LOG.exception(msg)
                    raise exception.HNASConnError(msg)
                elif 'Connection reset' in e.stderr:
                    msg = _("HNAS connection reset!")
                    LOG.exception(msg)
                    raise exception.HNASConnError(msg)
                else:
                    raise
Пример #18
0
    def _run_ssh(self, cmd_list, check_exit_code=True, attempts=1):
        utils.check_ssh_injection(cmd_list)
        # TODO(vhou): We'll have a common method in ssh_utils to take
        # care of this _run_ssh method.
        command = ' '.join(cmd_list)

        if not self.sshpool:
            self.sshpool = ssh_utils.SSHPool(
                self.target.get('san_ip'),
                self.target.get('san_ssh_port', 22),
                self.target.get('ssh_conn_timeout', 30),
                self.target.get('san_login'),
                password=self.target.get('san_password'),
                privatekey=self.target.get('san_private_key', ''),
                min_size=self.target.get('ssh_min_pool_conn', 1),
                max_size=self.target.get('ssh_max_pool_conn', 5),
            )
        last_exception = None
        try:
            with self.sshpool.item() as ssh:
                while attempts > 0:
                    attempts -= 1
                    try:
                        return processutils.ssh_execute(
                            ssh, command, check_exit_code=check_exit_code)
                    except Exception as e:
                        LOG.error(six.text_type(e))
                        last_exception = e
                        greenthread.sleep(random.randint(20, 500) / 100.0)
                try:
                    raise processutils.ProcessExecutionError(
                        exit_code=last_exception.exit_code,
                        stdout=last_exception.stdout,
                        stderr=last_exception.stderr,
                        cmd=last_exception.cmd)
                except AttributeError:
                    raise processutils.ProcessExecutionError(
                        exit_code=-1,
                        stdout="",
                        stderr="Error running SSH command",
                        cmd=command)

        except Exception:
            with excutils.save_and_reraise_exception():
                LOG.error(_LE("Error running SSH command: %s"), command)
Пример #19
0
    def _run_ssh(self, cmd_list, attempts=1):
        utils.check_ssh_injection(cmd_list)
        command = ' '.join(cmd_list)

        if not self.sshpool:
            password = self.configuration.san_password
            privatekey = self.configuration.san_private_key
            min_size = self.configuration.ssh_min_pool_conn
            max_size = self.configuration.ssh_max_pool_conn
            self.sshpool = ssh_utils.SSHPool(
                self.configuration.san_ip,
                self.configuration.san_ssh_port,
                self.configuration.ssh_conn_timeout,
                self.configuration.san_login,
                password=password,
                privatekey=privatekey,
                min_size=min_size,
                max_size=max_size)
        try:
            total_attempts = attempts
            with self.sshpool.item() as ssh:
                while attempts > 0:
                    attempts -= 1
                    try:
                        LOG.info(_('EQL-driver: executing "%s"') % command)
                        return self._ssh_execute(
                            ssh,
                            command,
                            timeout=self.configuration.eqlx_cli_timeout)
                    except processutils.ProcessExecutionError:
                        raise
                    except Exception as e:
                        LOG.exception(e)
                        greenthread.sleep(random.randint(20, 500) / 100.0)
                msg = (_("SSH Command failed after '%(total_attempts)r' "
                         "attempts : '%(command)s'") % {
                             'total_attempts': total_attempts,
                             'command': command
                         })
                raise exception.VolumeBackendAPIException(data=msg)

        except Exception:
            with excutils.save_and_reraise_exception():
                LOG.error(_("Error running SSH command: %s") % command)
 def _get_switch_data(self, cmd):
     stdin, stdout, stderr = None, None, None
     utils.check_ssh_injection([cmd])
     try:
         stdin, stdout, stderr = self.client.exec_command(cmd)
         switch_data = stdout.readlines()
     except paramiko.SSHException as e:
         msg = _("SSH Command failed with error '%(err)s' " "'%(command)s'") % {"err": e, "command": cmd}
         LOG.error(msg)
         raise exception.FCSanLookupServiceException(message=msg)
     finally:
         if stdin:
             stdin.flush()
             stdin.close()
         if stdout:
             stdout.close()
         if stderr:
             stderr.close()
     return switch_data
Пример #21
0
    def _run_ssh(self, cmd_list, check_exit_code=True, attempts=1):
        utils.check_ssh_injection(cmd_list)
        # TODO(vhou): We'll have a common method in ssh_utils to take
        # care of this _run_ssh method.
        command = ' '. join(cmd_list)

        if not self.sshpool:
            self.sshpool = ssh_utils.SSHPool(
                self.target.get('san_ip'),
                self.target.get('san_ssh_port', 22),
                self.target.get('ssh_conn_timeout', 30),
                self.target.get('san_login'),
                password=self.target.get('san_password'),
                privatekey=self.target.get('san_private_key', ''),
                min_size=self.target.get('ssh_min_pool_conn', 1),
                max_size=self.target.get('ssh_max_pool_conn', 5),)
        last_exception = None
        try:
            with self.sshpool.item() as ssh:
                while attempts > 0:
                    attempts -= 1
                    try:
                        return processutils.ssh_execute(
                            ssh, command, check_exit_code=check_exit_code)
                    except Exception as e:
                        LOG.error(six.text_type(e))
                        last_exception = e
                        greenthread.sleep(random.randint(20, 500) / 100.0)
                try:
                    raise processutils.ProcessExecutionError(
                        exit_code=last_exception.exit_code,
                        stdout=last_exception.stdout,
                        stderr=last_exception.stderr,
                        cmd=last_exception.cmd)
                except AttributeError:
                    raise processutils.ProcessExecutionError(
                        exit_code=-1, stdout="",
                        stderr="Error running SSH command",
                        cmd=command)

        except Exception:
            with excutils.save_and_reraise_exception():
                LOG.error(_LE("Error running SSH command: %s"), command)
Пример #22
0
    def _run_ssh(self, cmd_list, attempts=1):
        utils.check_ssh_injection(cmd_list)
        command = ' '. join(cmd_list)

        if not self.sshpool:
            password = self.configuration.san_password
            privatekey = self.configuration.san_private_key
            min_size = self.configuration.ssh_min_pool_conn
            max_size = self.configuration.ssh_max_pool_conn
            self.sshpool = ssh_utils.SSHPool(
                self.configuration.san_ip,
                self.configuration.san_ssh_port,
                self.configuration.ssh_conn_timeout,
                self.configuration.san_login,
                password=password,
                privatekey=privatekey,
                min_size=min_size,
                max_size=max_size)
        try:
            total_attempts = attempts
            with self.sshpool.item() as ssh:
                while attempts > 0:
                    attempts -= 1
                    try:
                        LOG.info(_LI('EQL-driver: executing "%s".'), command)
                        return self._ssh_execute(
                            ssh, command,
                            timeout=self.configuration.eqlx_cli_timeout)
                    except processutils.ProcessExecutionError:
                        raise
                    except Exception as e:
                        LOG.exception(e)
                        greenthread.sleep(random.randint(20, 500) / 100.0)
                msg = (_("SSH Command failed after '%(total_attempts)r' "
                         "attempts : '%(command)s'") %
                       {'total_attempts': total_attempts - attempts,
                        'command': command})
                raise exception.VolumeBackendAPIException(data=msg)

        except Exception:
            with excutils.save_and_reraise_exception():
                LOG.error(_LE('Error running SSH command: "%s".'), command)
Пример #23
0
    def _run_cmd(self, *args, **kwargs):
        """Runs a command on SMU using SSH.

        :returns: stdout and stderr of the command
        """
        if self.cluster_admin_ip0 is None:
            # Connect to SMU through SSH and run ssc locally
            args = (self.hnas_cmd, 'localhost') + args
        else:
            args = (self.hnas_cmd, '--smuauth', self.cluster_admin_ip0) + args

        utils.check_ssh_injection(args)
        command = ' '.join(args)
        command = command.replace('"', '\\"')

        if not self.sshpool:
            self.sshpool = ssh_utils.SSHPool(ip=self.mgmt_ip0,
                                             port=int(self.ssh_port),
                                             conn_timeout=None,
                                             login=self.ssh_username,
                                             password=self.ssh_pwd,
                                             privatekey=self.ssh_private_key)

        with self.sshpool.item() as ssh:
            try:
                out, err = putils.ssh_execute(ssh, command,
                                              check_exit_code=True)
                LOG.debug("command %(cmd)s result: out = "
                          "%(out)s - err = %(err)s",
                          {'cmd': self.hnas_cmd, 'out': out, 'err': err})
                return out, err
            except putils.ProcessExecutionError as e:
                if 'Failed to establish SSC connection' in e.stderr:
                    LOG.debug("SSC connection error!")
                    msg = _("Failed to establish SSC connection.")
                    raise exception.HNASConnError(msg)
                elif 'Connection reset' in e.stderr:
                    LOG.debug("HNAS connection reset!")
                    msg = _("HNAS has disconnected SSC")
                    raise exception.HNASConnError(msg)
                else:
                    raise
Пример #24
0
 def _get_switch_data(self, cmd):
     stdin, stdout, stderr = None, None, None
     utils.check_ssh_injection([cmd])
     try:
         stdin, stdout, stderr = self.client.exec_command(cmd)
         switch_data = stdout.readlines()
     except paramiko.SSHException as e:
         msg = (_("SSH Command failed with error '%(err)r' "
                  "'%(command)s'") % {'err': str(e), 'command': cmd})
         LOG.error(msg)
         raise exception.FCSanLookupServiceException(message=msg)
     finally:
         if (stdin):
             stdin.flush()
             stdin.close()
         if (stdout):
             stdout.close()
         if (stderr):
             stderr.close()
     return switch_data
Пример #25
0
                    raise
=======
            out, err = utils.execute(*args, **kwargs)
            LOG.debug("command %(cmd)s result: out = %(out)s - err = "
                      "%(err)s", {'cmd': cmd, 'out': out, 'err': err})
            return out, err
>>>>>>> refs/remotes/openstack/stable/kilo:cinder/volume/drivers/hds/hnas_backend.py
        else:
            if self.drv_configs['cluster_admin_ip0'] is None:
                # Connect to SMU through SSH and run ssc locally
                args = (cmd, 'localhost') + args
            else:
                args = (cmd, '--smuauth',
                        self.drv_configs['cluster_admin_ip0']) + args

            utils.check_ssh_injection(args)
            command = ' '.join(args)
            command = command.replace('"', '\\"')

            if not self.sshpool:
                server = self.drv_configs['mgmt_ip0']
                port = int(self.drv_configs['ssh_port'])
                username = self.drv_configs['username']
                # We only accept private/public key auth
                password = ""
                privatekey = self.drv_configs['ssh_private_key']
                self.sshpool = ssh_utils.SSHPool(server,
                                                 port,
                                                 None,
                                                 username,
                                                 password=password,
Пример #26
0
    def _run_ssh(self, cmd_list, check_exit_code=True, attempts=1):
        """
        override _run_ssh() method from san.SanDriver.
        It is important to set connection timeout to None
        if development_ssh_san_conn is enabled to close ssh
        connection per get(). Otherwise, keepalive without
        timeout on the ssh transport will cause the service
        greenthread to be stuck on the join during close()
        when put() is invoked.
        """
        if CONF.development_ssh_san_conn:
            LOG.debug("PowerVCSSHPool: close ssh connection after _run_ssh")
            timeout = None
            # periodic task and volume driver ssh execution may
            # be launched at the same time which could cause paramiko
            # authentication error due to the attempt to create
            # multiple paramiko ssh connection at the sametime.
            attempts = attempts if attempts > 3 else 3
        else:
            timeout = self.configuration.ssh_conn_timeout

        if not self.sshpool:
            password = self.configuration.san_password
            privatekey = self.configuration.san_private_key
            min_size = self.configuration.ssh_min_pool_conn
            max_size = self.configuration.ssh_max_pool_conn
            self.sshpool = PowerVCSSHPool(self.configuration.san_ip,
                                          self.configuration.san_ssh_port,
                                          timeout,
                                          self.configuration.san_login,
                                          password=password,
                                          privatekey=privatekey,
                                          min_size=min_size,
                                          max_size=max_size)

        utils.check_ssh_injection(cmd_list)
        command = ' '.join(cmd_list)

        last_exception = None
        pauses_between_attempts = 0
        try:
            while attempts > 0:
                attempts -= 1
                try:
                    with self.sshpool.item() as ssh:
                        # Record the command that we are about to execute,
                        # along with start time (in seconds)
                        self.pending_ssh[id(ssh)] = {'command': command,
                                                     'time': time.time()}
                        (out, err) = \
                            processutils.ssh_execute(ssh,
                                                     command,
                                                     check_exit_code=
                                                     check_exit_code)

                        # Remove the command from the list of commands we're
                        # executing.
                        del self.pending_ssh[id(ssh)]
                        return (out, err)
                except Exception as e:
                    LOG.info(e)
                    last_exception = e
                    if attempts > 0:
                        sleep_time = (random.randint(CONF.san_retry_min_delay,
                                                     CONF.san_retry_max_delay)
                                      + (pauses_between_attempts *
                                         CONF.san_retry_max_delay))
                        LOG.info(_("Waiting %(seconds)d seconds before next "
                                 "attempt...")
                                 % {'seconds': sleep_time})
                        greenthread.sleep(sleep_time)
                        pauses_between_attempts += 1
            try:
                raise processutils.ProcessExecutionError(
                    exit_code=last_exception.exit_code,
                    stdout=last_exception.stdout,
                    stderr=last_exception.stderr,
                    cmd=last_exception.cmd)
            except AttributeError:
                raise processutils.ProcessExecutionError(
                    exit_code=-1,
                    stdout="",
                    stderr="Error running SSH command",
                    cmd=command)

        except Exception:
            with excutils.save_and_reraise_exception():
                LOG.error(_("Error running SSH command: %s") % command)
Пример #27
0
    def _ssh_execute(self, cmd_list, check_exit_code=True, attempts=1):
        """Execute cli with status update.

        Executes CLI commands such as cfgsave where status return is expected.
        """
        utils.check_ssh_injection(cmd_list)
        command = ' '. join(cmd_list)

        if not self.sshpool:
            self.sshpool = ssh_utils.SSHPool(self.switch_ip,
                                             self.switch_port,
                                             None,
                                             self.switch_user,
                                             self.switch_pwd,
                                             min_size=1,
                                             max_size=5)
        stdin, stdout, stderr = None, None, None
        LOG.debug("Executing command via ssh: %s", command)
        last_exception = None
        try:
            with self.sshpool.item() as ssh:
                while attempts > 0:
                    attempts -= 1
                    try:
                        stdin, stdout, stderr = ssh.exec_command(command)
                        stdin.write("%s\n" % ZoneConstant.YES)
                        channel = stdout.channel
                        exit_status = channel.recv_exit_status()
                        LOG.debug("Exit Status from ssh: %s", exit_status)
                        # exit_status == -1 if no exit code was returned
                        if exit_status != -1:
                            LOG.debug('Result was %s', exit_status)
                            if check_exit_code and exit_status != 0:
                                raise processutils.ProcessExecutionError(
                                    exit_code=exit_status,
                                    stdout=stdout,
                                    stderr=stderr,
                                    cmd=command)
                            else:
                                return True
                        else:
                            return True
                    except Exception as e:
                        LOG.exception(_LE('Error executing SSH command.'))
                        last_exception = e
                        greenthread.sleep(random.randint(20, 500) / 100.0)
                LOG.debug("Handling error case after "
                          "SSH: %s", last_exception)
                try:
                    raise processutils.ProcessExecutionError(
                        exit_code=last_exception.exit_code,
                        stdout=last_exception.stdout,
                        stderr=last_exception.stderr,
                        cmd=last_exception.cmd)
                except AttributeError:
                    raise processutils.ProcessExecutionError(
                        exit_code=-1,
                        stdout="",
                        stderr="Error running SSH command",
                        cmd=command)
        except Exception as e:
            with excutils.save_and_reraise_exception():
                LOG.error(_LE("Error executing command via ssh: %s"), e)
        finally:
            if stdin:
                stdin.flush()
                stdin.close()
            if stdout:
                stdout.close()
            if stderr:
                stderr.close()
Пример #28
0
    def _ssh_execute(self, cmd_list, check_exit_code=True, attempts=1):
        """Execute cli with status update.

        Executes CLI commands where status return is expected.

        cmd_list is a list of commands, where each command is itself
        a list of parameters.  We use utils.check_ssh_injection to check each
        command, but then join then with " ; " to form a single command.
        """

        # Check that each command is secure
        for cmd in cmd_list:
            utils.check_ssh_injection(cmd)

        # Combine into a single command.
        command = ' ; '.join(map(lambda x: ' '.join(x), cmd_list))

        if not self.sshpool:
            self.sshpool = ssh_utils.SSHPool(self.switch_ip,
                                             self.switch_port,
                                             None,
                                             self.switch_user,
                                             self.switch_pwd,
                                             min_size=1,
                                             max_size=5)
        stdin, stdout, stderr = None, None, None
        LOG.debug("Executing command via ssh: %s", command)
        last_exception = None
        try:
            with self.sshpool.item() as ssh:
                while attempts > 0:
                    attempts -= 1
                    try:
                        stdin, stdout, stderr = ssh.exec_command(command)
                        greenthread.sleep(random.randint(20, 500) / 100.0)
                        channel = stdout.channel
                        exit_status = channel.recv_exit_status()
                        LOG.debug("Exit Status from ssh:%s", exit_status)
                        # exit_status == -1 if no exit code was returned
                        if exit_status != -1:
                            LOG.debug('Result was %s', exit_status)
                            if check_exit_code and exit_status != 0:
                                raise processutils.ProcessExecutionError(
                                    exit_code=exit_status,
                                    stdout=stdout,
                                    stderr=stderr,
                                    cmd=command)
                            else:
                                return True
                        else:
                            return True
                    except Exception as e:
                        msg = _("Exception: %s") % six.text_type(e)
                        LOG.error(msg)
                        last_exception = e
                        greenthread.sleep(random.randint(20, 500) / 100.0)
                LOG.debug("Handling error case after SSH:%s", last_exception)
                try:
                    raise processutils.ProcessExecutionError(
                        exit_code=last_exception.exit_code,
                        stdout=last_exception.stdout,
                        stderr=last_exception.stderr,
                        cmd=last_exception.cmd)
                except AttributeError:
                    raise processutils.ProcessExecutionError(
                        exit_code=-1,
                        stdout="",
                        stderr="Error running SSH command",
                        cmd=command)
        except Exception as e:
            with excutils.save_and_reraise_exception():
                msg = (_("Error executing command via ssh: %s") %
                       six.text_type(e))
                LOG.error(msg)
        finally:
            if stdin:
                stdin.flush()
                stdin.close()
            if stdout:
                stdout.close()
            if stderr:
                stderr.close()
Пример #29
0
 def test_check_ssh_injection(self):
     cmd_list = ['ssh', '-D', 'my_name@name_of_remote_computer']
     self.assertEqual(utils.check_ssh_injection(cmd_list), None)
Пример #30
0
    def _ssh_execute(self, cmd_list, check_exit_code=True, attempts=1):
        """Execute cli with status update.

        Executes CLI commands such as cfgsave where status return is expected.
        """
        utils.check_ssh_injection(cmd_list)
        command = ' '.join(cmd_list)

        if not self.sshpool:
            self.sshpool = ssh_utils.SSHPool(self.switch_ip,
                                             self.switch_port,
                                             None,
                                             self.switch_user,
                                             self.switch_pwd,
                                             self.switch_key,
                                             min_size=1,
                                             max_size=5)
        stdin, stdout, stderr = None, None, None
        LOG.debug("Executing command via ssh: %s", command)
        last_exception = None
        try:
            with self.sshpool.item() as ssh:
                while attempts > 0:
                    attempts -= 1
                    try:
                        stdin, stdout, stderr = ssh.exec_command(command)
                        stdin.write("%s\n" % zone_constant.YES)
                        channel = stdout.channel
                        exit_status = channel.recv_exit_status()
                        LOG.debug("Exit Status from ssh: %s", exit_status)
                        # exit_status == -1 if no exit code was returned
                        if exit_status != -1:
                            LOG.debug('Result was %s', exit_status)
                            if check_exit_code and exit_status != 0:
                                raise processutils.ProcessExecutionError(
                                    exit_code=exit_status,
                                    stdout=stdout,
                                    stderr=stderr,
                                    cmd=command)
                            else:
                                return True
                        else:
                            return True
                    except Exception as e:
                        LOG.exception('Error executing SSH command.')
                        last_exception = e
                        greenthread.sleep(random.randint(20, 500) / 100.0)
                LOG.debug("Handling error case after "
                          "SSH: %s", last_exception)
                try:
                    raise processutils.ProcessExecutionError(
                        exit_code=last_exception.exit_code,
                        stdout=last_exception.stdout,
                        stderr=last_exception.stderr,
                        cmd=last_exception.cmd)
                except AttributeError:
                    raise processutils.ProcessExecutionError(
                        exit_code=-1,
                        stdout="",
                        stderr="Error running SSH command",
                        cmd=command)
        except Exception as e:
            with excutils.save_and_reraise_exception():
                LOG.error("Error executing command via ssh: %s", e)
        finally:
            if stdin:
                stdin.flush()
                stdin.close()
            if stdout:
                stdout.close()
            if stderr:
                stderr.close()
Пример #31
0
 def test_check_ssh_injection(self):
     cmd_list = ['ssh', '-D', 'my_name@name_of_remote_computer']
     self.assertIsNone(utils.check_ssh_injection(cmd_list))
Пример #32
0
 def _run_ssh_aux(cmd, check_exit_code=True, attempts=1):
     utils.check_ssh_injection(cmd)
     if len(cmd) > 2 and cmd[1] == 'lssystem':
         cmd[1] = 'lssystem_aux'
     ret = self.sim.execute_command(cmd, check_exit_code)
     return ret
Пример #33
0
 def _run_ssh_aux(cmd, check_exit_code=True, attempts=1):
     utils.check_ssh_injection(cmd)
     if len(cmd) > 2 and cmd[1] == 'lssystem':
         cmd[1] = 'lssystem_aux'
     ret = self.sim.execute_command(cmd, check_exit_code)
     return ret
Пример #34
0
 def _ssh(self, cmd, check_exit_code=True):
     utils.check_ssh_injection(cmd)
     ret = self.fake_storage.execute_command(cmd, check_exit_code)
     return ret
    def _ssh_execute(self, cmd_list, check_exit_code=True, attempts=1):
        """Execute cli with status update.

        Executes CLI commands where status return is expected.

        cmd_list is a list of commands, where each command is itself
        a list of parameters.  We use utils.check_ssh_injection to check each
        command, but then join then with " ; " to form a single command.
        """

        # Check that each command is secure
        for cmd in cmd_list:
            utils.check_ssh_injection(cmd)

        # Combine into a single command.
        command = ' ; '.join(map(lambda x: ' '.join(x), cmd_list))

        if self.sshpool.get(self.switch_ip) is None:
            self.sshpool[self.switch_ip] = ssh_utils.SSHPool(self.switch_ip,
                                                             self.switch_port,
                                                             None,
                                                             self.switch_user,
                                                             self.switch_pwd,
                                                             min_size=1,
                                                             max_size=5)

        stdin, stdout, stderr = None, None, None
        LOG.debug("Executing command via ssh: %s", command)
        last_exception = None
        try:
            with self.sshpool.get(self.switch_ip).item() as ssh:
                while attempts > 0:
                    attempts -= 1
                    try:
                        stdin, stdout, stderr = ssh.exec_command(command)
                        greenthread.sleep(random.randint(20, 500) / 100.0)
                        channel = stdout.channel
                        exit_status = channel.recv_exit_status()
                        LOG.debug("Exit Status from ssh:%s", exit_status)
                        # exit_status == -1 if no exit code was returned
                        if exit_status != -1:
                            LOG.debug('Result was %s', exit_status)
                            if check_exit_code and exit_status != 0:
                                raise processutils.ProcessExecutionError(
                                    exit_code=exit_status,
                                    stdout=stdout,
                                    stderr=stderr,
                                    cmd=command)
                            else:
                                return True
                        else:
                            return True
                    except Exception as e:
                        msg = _("Exception: %s") % six.text_type(e)
                        LOG.error(msg)
                        last_exception = e
                        greenthread.sleep(random.randint(20, 500) / 100.0)
                LOG.debug("Handling error case after SSH:%s", last_exception)
                try:
                    raise processutils.ProcessExecutionError(
                        exit_code=last_exception.exit_code,
                        stdout=last_exception.stdout,
                        stderr=last_exception.stderr,
                        cmd=last_exception.cmd)
                except AttributeError:
                    raise processutils.ProcessExecutionError(
                        exit_code=-1,
                        stdout="",
                        stderr="Error running SSH command",
                        cmd=command)
        except Exception as e:
            with excutils.save_and_reraise_exception():
                msg = (_("Error executing command via ssh: %s") %
                       six.text_type(e))
                LOG.error(msg)
        finally:
            if stdin:
                stdin.flush()
                stdin.close()
            if stdout:
                stdout.close()
            if stderr:
                stderr.close()
Пример #36
0
    def run_cmd(self, cmd, ip0, user, pw, *args, **kwargs):
        """Run a command on SMU or using SSH

        :param cmd: the command that will be run on SMU
        :param ip0: string IP address of controller
        :param user: string user authentication for array
        :param pw: string password authentication for array
        :returns: formated string with version information
        """
        LOG.debug("Enable ssh: %s", six.text_type(self.drv_configs["ssh_enabled"]))

        if self.drv_configs["ssh_enabled"] != "True":
            # Direct connection via ssc
            args = (cmd, "-u", user, "-p", pw, ip0) + args

            try:
                out, err = utils.execute(*args, **kwargs)
                LOG.debug(
                    "command %(cmd)s result: out = %(out)s - err = " "%(err)s", {"cmd": cmd, "out": out, "err": err}
                )
                return out, err
            except putils.ProcessExecutionError as e:
                if "Failed to establish SSC connection" in e.stderr:
                    LOG.debug("SSC connection error!")
                    msg = _("Failed to establish SSC connection.")
                    raise exception.HNASConnError(msg)
                else:
                    raise putils.ProcessExecutionError

        else:
            if self.drv_configs["cluster_admin_ip0"] is None:
                # Connect to SMU through SSH and run ssc locally
                args = (cmd, "localhost") + args
            else:
                args = (cmd, "--smuauth", self.drv_configs["cluster_admin_ip0"]) + args

            utils.check_ssh_injection(args)
            command = " ".join(args)
            command = command.replace('"', '\\"')

            if not self.sshpool:
                server = self.drv_configs["mgmt_ip0"]
                port = int(self.drv_configs["ssh_port"])
                username = self.drv_configs["username"]
                # We only accept private/public key auth
                password = ""
                privatekey = self.drv_configs["ssh_private_key"]
                self.sshpool = ssh_utils.SSHPool(server, port, None, username, password=password, privatekey=privatekey)

            with self.sshpool.item() as ssh:

                try:
                    out, err = putils.ssh_execute(ssh, command, check_exit_code=True)
                    LOG.debug(
                        "command %(cmd)s result: out = " "%(out)s - err = %(err)s", {"cmd": cmd, "out": out, "err": err}
                    )
                    return out, err
                except putils.ProcessExecutionError as e:
                    if "Failed to establish SSC connection" in e.stderr:
                        LOG.debug("SSC connection error!")
                        msg = _("Failed to establish SSC connection.")
                        raise exception.HNASConnError(msg)
                    else:
                        raise putils.ProcessExecutionError
Пример #37
0
 def _ssh(self, cmd, check_exit_code=True):
     utils.check_ssh_injection(cmd)
     ret = self.fake_storage.execute_command(cmd, check_exit_code)
     return ret