示例#1
0
文件: general.py 项目: ysoldak/syco
def shell_exec(command, user="", cwd=None, events=None, output=True):
    '''
    Execute a shell command using pexpect, and writing verbose affected output.

    '''
    # Build command to execute
    args=[]
    if (user):
        args.append(user)
    args.append('-c ' + command)

    if (output):
        app.print_verbose(BOLD +"Command: su " + RESET + user + " -c '" + command + "'")

    # Setting default events
    if events is None:
        events = {}

    events["Verify the SYCO master password:"******"su", args, cwd=cwd)

    if (not output):
        out.disable_output()

    if (output):
        app.print_verbose("---- Result ----", 2)
    stdout = ""
    index = 0
    while (index < num_of_events+1):
        index = out.expect(keys, timeout=3600)
        stdout += out.before

        if index >= 0 and index < num_of_events:
            if (inspect.isfunction(value[index])):
                out.send(str(value[index]())  + "\n")
            else:
                out.send(value[index])
        elif index == num_of_events:
            app.print_error("Catched a timeout from pexpect.expect, lets try again.")

    if (out.exitstatus and output):
        app.print_error("Invalid exitstatus %d" % out.exitstatus)

    if (out.signalstatus and output):
        app.print_error("Invalid signalstatus %d - %s" % out.signalstatus, out.status)

    # An extra line break for the looks.
    if (output and stdout and app.options.verbose >= 2):
        print("\n"),

    out.close()

    return stdout
示例#2
0
文件: general.py 项目: ysoldak/syco
def shell_exec(command, user="", cwd=None, events=None, output=True):
    '''
    Execute a shell command using pexpect, and writing verbose affected output.

    '''
    # Build command to execute
    args = []
    if (user):
        args.append(user)
    args.append('-c ' + command)

    if (output):
        app.print_verbose(BOLD + "Command: su " + RESET + user + " -c '" +
                          command + "'")

    # Setting default events
    if events is None:
        events = {}

    events["Verify the SYCO master password:"******"su", args, cwd=cwd)

    if (not output):
        out.disable_output()

    if (output):
        app.print_verbose("---- Result ----", 2)
    stdout = ""
    index = 0
    while (index < num_of_events + 1):
        index = out.expect(keys, timeout=3600)
        stdout += out.before

        if index >= 0 and index < num_of_events:
            if (inspect.isfunction(value[index])):
                out.send(str(value[index]()) + "\n")
            else:
                out.send(value[index])
        elif index == num_of_events:
            app.print_error(
                "Catched a timeout from pexpect.expect, lets try again.")

    if (out.exitstatus and output):
        app.print_error("Invalid exitstatus %d" % out.exitstatus)

    if (out.signalstatus and output):
        app.print_error("Invalid signalstatus %d - %s" % out.signalstatus,
                        out.status)

    # An extra line break for the looks.
    if (output and stdout and app.options.verbose >= 2):
        print("\n"),

    out.close()

    return stdout
示例#3
0
        # initialize shell machine
        if 'ssh' in command and self._play_context.prompt:
            state.shellmachine = self.shellmachine.iterate(State.Start)
        elif 'ssh' in command and self._play_context.become and self._play_contxt.success_key:
            state.shellmachine = self.shellmachine.iterate(State.Escalation)
        else:
            state.shellmachine = self.shellmachine.iterate(State.Exit)
        state.shellmachine.next()
        #M.set(State.Start)          # 'ssh' in cmd and self._play_context.prompt
        #M.set(State.Escalation)     # 'ssh' in cmd and self._play_context.become and self._play_context.success_key
        #M.set(State.Exit)           # 'ssh' not in cmd

        #state.P = expect.spawn(_monitor_command(self, state, Output), command, stderr=_monitor_output(state, Error))
        self._display.debug("Spawning {!r}".format(command))
        state.P = expect.spawn(monitor(self, state), command, show=True)
        return state

    def exec_command(self, cmd, in_data=None, sudoable=True):
        super(Connection, self).exec_command(cmd=cmd, in_data=in_data, sudoable=sudoable)
        self._display.vvv("ESTABLISH SSH CONNECTION FOR USER: {0}".format(self._play_context.remote_user), host=self._play_context.remote_addr)

        command = self._get_command(Executable['plink']) + ' "{:s}"'.format(cmd.replace('"', '\\"'))
        self._display.vvvvv("Executing {!r}".format(command))
        st = self.plink(command)
        self._display.debug("Waiting")

        returncode = st.P.wait()

        self._display.debug("Done {!r}".format(returncode))
        assert not st.P.running