Golang:ssh运行远程脚本:没有这样的文件或目录

I'm using ssh module to run shell scripts on remote machine:

// ssh-run.go
package main
import (
    "bytes"
    "flag"
    "fmt"
    "log"
    "time"

    "golang.org/x/crypto/ssh"
)

var (
    flagUser = flag.String("user", "", "")
    flagPwd = flag.String("pwd", "", "")
    flagHost = flag.String("host", "", "")
    flagCmd = flag.String("cmd", "", "")
)

func main() {
    flag.Parse()
    log.SetFlags(log.Lshortfile | log.LstdFlags)

    cfg := ssh.ClientConfig {
        User: *flagUser,
        Auth: []ssh.AuthMethod(ssh.Password(*flagPwd)),
    }

    // skip err checking for short code
    conn, _ := ssh.Dial("tcp", fmt.Sprintf("%s:22", *flagHost), &cfg)
    defer conn.Close()
    ss, _ := conn.NewSession()
    defer ss.Close()

    var out bytes.Buffer
    ss.Stdout = &out
    ss.Stderr = &out
    ss.Run(*flagCmd)
    fmt.Printf("err: %v, out: %v
", err, out.String())
}

On a remote Centos server(sshd is running), I put a simple Ping script under home path, and I run the demo:

go run ssh-run.go -cmd "/bin/bash /home/me/try_ping.sh" -host 172.17.0.2 -pwd 123456 -user me

I got the error:

err: Process exited with status 127, out: /bin/bash: /home/me/try_ping.sh: No such file or directory

But when the remote server is a Ubuntu server, the Ping script works well.

What is the difference between Centos and Ubuntu when facing remote script running? I need to run remote scripts under many UNIX-like OS, How to fix the gap between them?

I go the reason. Normally, when we need to pass a long command to remote server via SSH, say sh /path/to/script.sh, we can run it via(single quote also works):

ssh user@1.2.3.4 "sh /path/to/script.sh"

here, we use double quote to disable shell parameter split.

While in my true golang code (above code is cut from my project and use flags for testing.), I also used double quote, and the golang SSH package also send the tow double quote to remote SSH server, then remote SSH don't know how to locate files prefixed with double quote.

The annoy things is that, the error message do not carry the double quote, blind me about the double quote.

Thanks @JimB :)