Skip to content

Commit

Permalink
Fix ssh timeout (#483)
Browse files Browse the repository at this point in the history
* fix ssh timeout

* fix ssh timeout

* fix ssh timeout

* fix ssh timeout

* send sig when timeout
  • Loading branch information
suchen-sci authored Feb 27, 2024
1 parent 18b2973 commit a9c3eed
Showing 1 changed file with 18 additions and 7 deletions.
25 changes: 18 additions & 7 deletions probe/ssh/ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ package ssh

import (
"bytes"
"context"
"fmt"
"net"

Expand Down Expand Up @@ -140,10 +141,10 @@ func (s *Server) DoProbe() (bool, string) {
message := "SSH Command has been Run Successfully!"

if err != nil {
if _, ok := err.(*ssh.ExitMissingError); ok {
s.exitCode = UnknownExitCode // Error: remote server does not send an exit status
} else if e, ok := err.(*ssh.ExitError); ok {
if e, ok := err.(*ssh.ExitError); ok {
s.exitCode = e.ExitStatus()
} else {
s.exitCode = UnknownExitCode
}
log.Errorf("[%s / %s] %v", s.ProbeKind, s.ProbeName, err)
status = false
Expand Down Expand Up @@ -259,11 +260,21 @@ func (s *Server) RunSSHCmd() (string, error) {
var stdoutBuf, stderrBuf bytes.Buffer
session.Stdout = &stdoutBuf
session.Stderr = &stderrBuf
if err := session.Run(env + global.CommandLine(s.Command, s.Args)); err != nil {
return stderrBuf.String(), err
}

return stdoutBuf.String(), nil
errCh := make(chan error, 1)
go func() {
errCh <- session.Run(env + global.CommandLine(s.Command, s.Args))
}()

ctx, cancel := context.WithTimeout(context.Background(), s.Timeout())
defer cancel()
select {
case <-ctx.Done():
session.Signal(ssh.SIGINT)
return fmt.Sprintf("timeout after %s", s.Timeout()), ctx.Err()
case err := <-errCh:
return stdoutBuf.String(), err
}
}

// ExportMetrics export shell metrics
Expand Down

0 comments on commit a9c3eed

Please sign in to comment.