func (s *sshConn) runSudoCommand(sess *ssh.Session, stdin io.Writer, cmd string) error { // Wrap the output if s.DebugEnabled() { debugOutWriter := util.NewDebugLogWriter("SSH OUT:", s.Context) if sess.Stdout != nil { sess.Stdout = io.MultiWriter(sess.Stdout, debugOutWriter) } else { sess.Stdout = debugOutWriter } debugErrWriter := util.NewDebugLogWriter("SSH ERR:", s.Context) if sess.Stderr != nil { sess.Stderr = io.MultiWriter(sess.Stderr, debugErrWriter) } else { sess.Stderr = debugErrWriter } } // We need a checker to enter the password passwordTyper := util.NewExpectListener(stdin, shell.SudoPasswordPromptMatch, s.server.SSH.Pass+"\n") if sess.Stdout == nil { sess.Stdout = passwordTyper } else { sess.Stdout = io.MultiWriter(sess.Stdout, passwordTyper) } if sess.Stderr == nil { sess.Stderr = passwordTyper } else { sess.Stderr = io.MultiWriter(sess.Stderr, passwordTyper) } if err := sess.Run("sudo -S " + cmd); err != nil { return fmt.Errorf("Error running command %v: %v", cmd, err) } return nil }
func WrapCommandOutput(ctx *context.Context, cmd *exec.Cmd) *exec.Cmd { // If we are verbose, we want to wrap stdout/stderr to log writes if ctx.DebugEnabled() { AppendStdoutWriter(cmd, util.NewDebugLogWriter("SHELL OUT:", ctx)) AppendStderrWriter(cmd, util.NewDebugLogWriter("SHELL ERR:", ctx)) } return cmd }