'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.'

def _ssh_execute(self, cmd_list, check_exit_code=True, attempts=1):

    for cmd in cmd_list:
        utils.check_ssh_injection(cmd)
    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)
                    channel = stdout.channel
                    exit_status = channel.recv_exit_status()
                    LOG.debug('Exit    Status    from    ssh:    %s', exit_status)
                    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:
        with excutils.save_and_reraise_exception():
            LOG.exception('Error    executing    command    via    ssh.')
    finally:
        if stdin:
            stdin.flush()
            stdin.close()
        if stdout:
            stdout.close()
        if stderr:
            stderr.close()
