From 77088e03a769d456d2c08dd22444d0fdecae1a10 Mon Sep 17 00:00:00 2001 From: Axel Wagner Date: Thu, 25 Oct 2018 12:49:32 +0200 Subject: [PATCH] Forward correct exit status to client (#1) --- ssh.go | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/ssh.go b/ssh.go index 1edcf0a..d89d8b0 100644 --- a/ssh.go +++ b/ssh.go @@ -194,13 +194,16 @@ func (s *session) request(ctx context.Context, req *ssh.Request) error { }() go func() { - // TODO: correctly pass on the exit code, currently it is always 255 if err := cmd.Wait(); err != nil { log.Printf("err: %v", err) } + status := make([]byte, 4) + if ws, ok := cmd.ProcessState.Sys().(syscall.WaitStatus); ok { + binary.BigEndian.PutUint32(status, uint32(ws.ExitStatus())) + } // See https://tools.ietf.org/html/rfc4254#section-6.10 - if _, err := s.channel.SendRequest("exit-status", false /* wantReply */, []byte("\x00\x00\x00\x00")); err != nil { + if _, err := s.channel.SendRequest("exit-status", false /* wantReply */, status); err != nil { log.Printf("err2: %v", err) } s.channel.Close()