apc-p15-tool/pkg/app/ssh_response.go

35 lines
774 B
Go
Raw Normal View History

package app
import (
"bufio"
"fmt"
"io"
)
2024-02-05 23:25:55 +00:00
// sshCheckResponse reads the output from the remote and returns an error
// if the remote output was not 0
2024-02-05 23:25:55 +00:00
func sshCheckResponse(remoteOutPipe io.Reader) error {
buffer := make([]uint8, 1)
_, err := remoteOutPipe.Read(buffer)
if err != nil {
2024-02-05 23:25:55 +00:00
return fmt.Errorf("ssh: failed to make read output buffer (%w)", err)
}
responseType := buffer[0]
message := ""
if responseType > 0 {
bufferedReader := bufio.NewReader(remoteOutPipe)
message, err = bufferedReader.ReadString('\n')
if err != nil {
2024-02-05 23:25:55 +00:00
return fmt.Errorf("ssh: failed to read output buffer (%w)", err)
}
}
// if not 0 (aka OK)
if responseType != 0 {
2024-02-05 23:25:55 +00:00
return fmt.Errorf("ssh: remote returned error (%d: %s)", responseType, message)
}
return nil
}