Skip to content

Commit

Permalink
fix: 兼容没有sudo命令的服务端
Browse files Browse the repository at this point in the history
  • Loading branch information
guoxi.wang committed Nov 29, 2023
1 parent cee5629 commit 431e16b
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
1 change: 1 addition & 0 deletions pkg/transport/ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ type Session struct {
Client *Client
sshSession *ssh.Session
stdin io.WriteCloser
hasSudo bool
}

/*
Expand Down
27 changes: 25 additions & 2 deletions pkg/transport/sudo.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,33 @@ func (w *sudoWriter) Write(p []byte) (int, error) {
return w.b.Write(p)
}

func (s *Session) hasSudoCommand() bool {
if s.hasSudo {
return true
}

ss, err := s.Client.NewPty()
if err != nil {
return false
}

defer ss.Close()

_, err = ss.Output("sudo -V")
if err != nil {
return false
}

s.hasSudo = true

return true
}

func (s *Session) Sudo(cmd, passwd string) ([]byte, error) {
if cmd == "" {
if cmd == "" || !s.hasSudoCommand() {
return s.Output(cmd)
}

cmd = "sudo -p " + sudoPwPrompt + " -S " + cmd

// Use the sudoRW struct to handle the interaction with sudo and capture the
Expand Down Expand Up @@ -71,7 +94,7 @@ func (s *Session) SudoContext(ctx context.Context, cmd, passwd string) ([]byte,
}
}()

if cmd == "" {
if cmd == "" || !s.hasSudoCommand() {
return s.Output(cmd)
}
cmd = "sudo -p " + sudoPwPrompt + " -S " + cmd
Expand Down

0 comments on commit 431e16b

Please sign in to comment.