I am trying to get the error info when running a command from ssh. Here is the example:
package main
import (
"golang.org/x/crypto/ssh"
"net"
)
func main() {
config := &ssh.ClientConfig{
User: "xx",
Auth: []ssh.AuthMethod{
ssh.Password("xxx"),
},
HostKeyCallback: func(hostname string, remote net.Addr, key ssh.PublicKey) error {
return nil
},
}
sshClient, err := ssh.Dial("tcp", "x.x.x.x:22", config)
if err != nil {
panic(err.Error())
}
session, err := sshClient.NewSession()
if err != nil {
panic(err.Error())
}
data, err := session.CombinedOutput("ls2")
if err != nil {
// This prints : error = Process exited with status 127
// Pos1
print("error = " + err.Error())
return
}
// Pos2
content := string(data)
println(content)
}
I test a wrong command: ls2, and I want to get the actual error info "-bash: ls2: command not found" at Pos2. But this test program failed at Pos1, and print "error = Process exited with status 127", which is not what I want, because I need the actual info.