dongxing9219
dongxing9219
2017-10-26 10:03

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

已采纳

I'm invoking 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())

And I'm getting this error:

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]

Tried to explicitly set the full path of ssh in rsh.

The output of fmt.Println(cmd) seems correct:

&{/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>}

What's wrong with my exec.Command?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

1条回答

  • dty63504 dty63504 4年前

    You are merging two arguments into one (--rsh and /usr/bin/ssh ...). Split them and it should work:

    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))
    
    点赞 评论 复制链接分享

相关推荐