douang1243
douang1243
2017-01-24 16:49

使用sftp <<通过golang os / exec输入

  • IT行业问题
  • 计算机技术
  • it技术
  • 编程语言问答
  • 互联网问答
已采纳

What I want to do is exporting a file from my server via SFTP, in golang.

Here is the troubling code

cmd = exec.Command("sftp", "login@sftp.com", `INPUT
    cd /some/path
    put file.gz
    quit
    INPUT`)
cmd.Stderr = &stderr
err = cmd.Run()
if err != nil {
    fmt.Println(stderr.String())
    os.Exit(1)
}
fmt.Println("done")

It gets to done but doesn't import file.gz.

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

1条回答

  • dpa84373 dpa84373 4年前

    I finally found the solution.

    cmd = exec.Command("sftp", "login@sftp.com")
    cmd.Stdin = strings.NewReader(`cd some/path
    put file.gz
    quit`)
    cmd.Stderr = &stderr
    err = cmd.Start()
    if err != nil {
        fmt.Println(stderr.String())
        fmt.Println(err)
        os.Exit(1)
    }
    err = cmd.Wait()
    

    I set the commands I need to do once I'm in the STFP by setting cmd.Stdin

    cmd.Start() and cmd.Wait() starts the command and stops when the whole command is done.

    点赞 评论 复制链接分享

为你推荐