运行 通过 exec.Command 使用 rsh 选项进行 rsync

Running rsync with rsh option via exec.Command

我正在调用 in exec.Command:

rsh := fmt.Sprintf("--rsh=\"/usr/bin/ssh -p %s -o StrictHostKeyChecking=no -l %s\"", *destPort, *destUserName)
cmd := exec.Command("rsync", "-avz", rsh, from, fmt.Sprintf("%s:%s", *destHost, to))
fmt.Println(cmd)
var outb, errb bytes.Buffer
cmd.Stdout = &outb
cmd.Stderr = &errb

err = cmd.Run()
fmt.Println(outb.String())
fmt.Println(errb.String())

我收到这个错误:

rsync: Failed to exec /usr/bin/ssh -p 1999 -o StrictHostKeyChecking=no -l root: No such file or directory (2)
rsync error: error in IPC code (code 14) at pipe.c(84) [sender=3.0.9]
rsync: connection unexpectedly closed (0 bytes received so far) [sender]
rsync error: error in IPC code (code 14) at io.c(605) [sender=3.0.9]

试图在 rsh 中明确设置 ssh 的完整路径。

fmt.Println(cmd) 的输出似乎是正确的:

&{/usr/bin/rsync [rsync -avz --rsh="/usr/bin/ssh -p 1999 -o StrictHostKeyChecking=no -l root" /var/www/html/atlaserpsolution.com/production/altractors h3.ftp.datagix.com:/var/www/vhosts/atlas.ns513.datagix.com/atlaserp/documents/altractors] []  <nil> <nil> <nil> [] <nil> <nil> <nil> <nil> <nil> false [] [] [] [] <nil> <nil>}

我的 exec.Command 怎么了?

您正在将两个参数合并为一个参数(--rsh/usr/bin/ssh ...)。拆分它们,它应该可以工作:

rsh := fmt.Sprintf("/usr/bin/ssh -p %s -o StrictHostKeyChecking=no -l %s", *destPort, *destUserName)
cmd := exec.Command("rsync", "-avz", "--rsh", rsh, from, fmt.Sprintf("%s:%s", *destHost, to))