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())
}
sshd
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?