dsxmwin86342 2013-09-27 05:52
浏览 72

在SSH客户端中为Golang创建文件

I have been struggling with this for around a week now. I have tried an scp client for golang but was informed that the code was broken, and the contributed "fixed" code didn't work as well. Anyway, i'm giving up on file transferring and decided to just use ssh to create files and save on the remote machine.

I successfully ran the ssh client in golang before going into scp route so this may be a good solution.

In the ssh client it just executed the "ls" command and I am aware that there is a "touch" command to create a file.

var b bytes.Buffer
session.Stdout = &b
if err := session.Run("ls"); err != nil {
    panic("Failed to run: " + err.Error())
}
fmt.Println(b.String())

Ultimately what I would like to achieve is read a local file maybe through os.Open(localfile) get it's contents then create a new file on the remote machine with the "touch" command then edit it and stick in the contents from the local file earlier. Or maybe there is a command that will make a new file and prepare it's contents already?

This looked promising, although this code gives me an error but from my observation, this would create a file called "testfile" with content of "123456789 " then I think upload it using session.Run("/usr/bin/scp -qrt ./");?

    go func() {
    w, _ := session.StdinPipe()
    defer w.Close()
    content := "123456789
"
    fmt.Fprintln(w, "C0644", len(content), "testfile")
    fmt.Fprint(w, content)
    fmt.Fprint(w, "\x00") // 传输以\x00结束
}()
if err := session.Run("/usr/bin/scp -qrt ./"); err != nil {
    panic("Failed to run: " + err.Error())
}

The above code can be found in my previous question about scp in golang

I'm not sure if this is what the code does, as I can't run it because of the error. I've tried to find out what the /usr/bin/scp -qrt ./ command does although couldn't exactly understand it.

I've been on this project for a week now and is very frustrating. Any help would be greatly appreciated. Thanks in advance.

  • 写回答

1条回答 默认 最新

  • dqr3883 2013-10-28 07:59
    关注

    I'm starting to write an scp client in go, and I came across your question in the process. I've got a lot closer to understanding it, so for now I'll just give you some direction to do this yourself. If I get any further I'll update my answer.

    1. Your question about scp -qrt: this is 'remote scp' in 'sink' or 'to' mode. The scp client relies on another copy of scp being available on the remote host: scp -t being 'to'/sink mode, and scp -f being 'from'/source mode. Essentially scp -f will send back a short file 'header', followed by the contents of the file itself. scp -t simply expects to receive the same thing, for local storage.

    2. For a good starting point (actually this gives pretty-much everything you need), read this Oracle post. It explains in detail what scp is doing.

    3. For a good example in a similar language, see this python implementation of scp

    That should set you on your way. Cheers

    评论

报告相同问题?

悬赏问题

  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 MATLAB中streamslice问题
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序