Beispiel #1
0
    def __init__(self, connection, sudo_password, cmd_object=None, cmd_class_name=None, cmd_params=None, prompt=None,
                 newline_chars=None, runner=None):
        super(Sudo, self).__init__(connection=connection, prompt=prompt, newline_chars=newline_chars, runner=runner)
        self.sudo_password = sudo_password
        self.cmd_object = cmd_object
        self._sent_sudo_password = False
        self._sent_command_string = False
        self.newline_seq = "\r\n"
        if cmd_object and cmd_class_name:
            self.set_exception(CommandFailure(self, "both 'cmd_object' and 'cmd_class_name' parameters provided. Please specify only one. "))
            return

        if cmd_class_name:
            params = dict()
            if cmd_params is not None:
                params = cmd_params.copy()
            params["connection"] = connection
            params['prompt'] = prompt
            params["newline_chars"] = newline_chars
            try:
                self.cmd_object = create_object_from_name(cmd_class_name, cmd_params)
            except Exception as ex:
                self.set_exception(ex)
        else:
            if not self.cmd_object:
                self.set_exception(CommandFailure(self,
                                                  "Neither 'cmd_class_name' nor 'cmd_object' was provided to Sudo constructor. Please specific parameter."))
Beispiel #2
0
    def __init__(self,
                 connection,
                 password,
                 cmd_object=None,
                 cmd_class_name=None,
                 cmd_params=None,
                 prompt=None,
                 newline_chars=None,
                 runner=None,
                 encrypt_password=True):
        """
        Constructs object for Unix command sudo.

        :param connection: Moler connection to device, terminal when command is executed.
        :param password: password for sudo.
        :param cmd_object: object of command. Pass this object or cmd_class_name.
        :param cmd_class_name: full (with package) class name. Pass this name or cmd_object.
        :param cmd_params: params for cmd_class_name. If cmd_object is passed this parameter is ignored.
        :param prompt: prompt (on system where command runs).
        :param newline_chars: Characters to split lines - list.
        :param runner: Runner to run command.
        :param encrypt_password: If True then * will be in logs when password is sent, otherwise plain text.
        """
        super(Sudo, self).__init__(connection=connection,
                                   prompt=prompt,
                                   newline_chars=newline_chars,
                                   runner=runner)
        self.password = password
        self.cmd_object = cmd_object
        self.encrypt_password = encrypt_password
        self._sent_sudo_password = False
        self._sent_command_string = False
        self.newline_seq = "\n"
        if cmd_object and cmd_class_name:
            self.set_exception(
                CommandFailure(
                    self,
                    "both 'cmd_object' and 'cmd_class_name' parameters provided. Please specify only one. "
                ))
            return

        if cmd_class_name:
            params = copy_dict(cmd_params)
            params["connection"] = connection
            params['prompt'] = prompt
            params["newline_chars"] = newline_chars
            try:
                self.cmd_object = create_object_from_name(
                    cmd_class_name, params)
            except Exception as ex:
                self.set_exception(ex)
        else:
            if not self.cmd_object:
                self.set_exception(
                    CommandFailure(
                        self,
                        "Neither 'cmd_class_name' nor 'cmd_object' was provided to Sudo constructor. Please specific parameter."
                    ))
Beispiel #3
0
    def _build_command_object(self):
        """
        Builds command object from passed parameters to sudo command.

        :return: None
        """
        self._validate_passed_object_or_command_parameters()
        if self.cmd_object:
            return
        else:
            params = copy_dict(self.cmd_params)
            params["connection"] = self.connection
            params['prompt'] = self._re_prompt
            params["newline_chars"] = self._newline_chars
            self.cmd_object = create_object_from_name(self.cmd_class_name,
                                                      params)
Beispiel #4
0
    def _validate_start(self, *args, **kwargs):
        """
        Validates internal data before start.

        :param args: args passed to super _validate_start
        :param kwargs: kwargs passed to super _validate_start
        :return: None.
        :raises: CommandFailure if error in command settings.
        """
        super(Sudo, self)._validate_start(*args, **kwargs)
        if self.cmd_object and self.cmd_class_name:
            # _validate_start is called before running command on connection, so we raise exception instead
            # of setting it
            raise CommandFailure(
                self,
                "both 'cmd_object' and 'cmd_class_name' parameters provided. Please specify only one."
            )

        if self.cmd_class_name:
            params = copy_dict(self.cmd_params)
            params["connection"] = self.connection
            params['prompt'] = self._re_prompt
            params["newline_chars"] = self._newline_chars
            self.cmd_object = create_object_from_name(self.cmd_class_name,
                                                      params)
        else:
            if not self.cmd_object:
                # _validate_start is called before running command on connection, so we raise exception
                # instead of setting it
                raise CommandFailure(
                    self,
                    "Neither 'cmd_class_name' nor 'cmd_object' was provided to Sudo constructor."
                    "Please specific parameter.")
        if self.cmd_object and self.cmd_object.done():
            # _validate_start is called before running command on connection, so we raise exception
            # instead of setting it
            raise CommandFailure(
                self,
                "Not allowed to run again the embeded command (embeded command is done): {}."
                .format(self.cmd_object))
        self.ret_required = self.cmd_object.ret_required
        if self.timeout_from_embedded_command:
            self.timeout = self.cmd_object.timeout