英文:
Creating a file in ssh client for golang
问题
我已经为大约一周的时间在努力解决这个问题了。我尝试了一个用于golang的scp客户端,但被告知代码有问题,而贡献的“修复”代码也不起作用。无论如何,我放弃了文件传输,决定只使用ssh在远程机器上创建文件并保存。
在进入scp路线之前,我成功地在golang中运行了ssh客户端,所以这可能是一个好的解决方案。
在ssh客户端中,它只执行了"ls"命令,我知道有一个"touch"命令可以创建文件。
var b bytes.Buffer
session.Stdout = &b
if err := session.Run("ls"); err != nil {
panic("Failed to run: " + err.Error())
}
fmt.Println(b.String())
最终,我想要实现的是读取一个本地文件,比如通过os.Open(localfile)
获取它的内容,然后使用"touch"命令在远程机器上创建一个新文件,然后编辑它并将之前的本地文件内容插入其中。或者也许有一个命令可以创建一个新文件并准备好它的内容?
下面的代码看起来很有希望,尽管这段代码给我报错了,但从我的观察来看,它将创建一个名为"testfile"的文件,内容为"123456789\n",然后我认为可以使用session.Run("/usr/bin/scp -qrt ./");
来上传它。
go func() {
w, _ := session.StdinPipe()
defer w.Close()
content := "123456789\n"
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())
}
上面的代码可以在我之前关于golang中scp的问题中找到。
我不确定这段代码是否能够实现我想要的功能,因为由于错误的原因我无法运行它。我尝试了解"/usr/bin/scp -qrt ./"命令的作用,但无法完全理解。
我已经在这个项目上花了一周的时间,非常沮丧。非常感谢您提前的帮助。
英文:
I have been struggling with this for around a week now. I have tried an <a href="https://gist.github.com/jedy/3357393">scp client for golang</a> 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 <a href="https://gist.github.com/linuz-ly/4148437">golang</a> 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\n" then I think upload it using session.Run("/usr/bin/scp -qrt ./");
?
go func() {
w, _ := session.StdinPipe()
defer w.Close()
content := "123456789\n"
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 <a href="https://stackoverflow.com/questions/19021964/scp-client-in-golang/19022612?noredirect=1#19022612">question about scp in golang</a>
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
得分: 1
我正在用Go语言编写一个SCP客户端,并在这个过程中遇到了你的问题。我已经更接近理解它了,所以现在我只给你一些方向,让你自己完成。如果我有进一步的了解,我会更新我的答案。
-
你关于
scp -qrt
的问题:这是远程SCP的“接收”或“目标”模式。SCP客户端依赖于远程主机上另一个可用的scp副本:scp -t
是“目标”/“接收”模式,scp -f
是“源”/“发送”模式。基本上,scp -f
会发送一个短文件“头”,然后是文件本身的内容。scp -t
只是期望接收相同的内容,用于本地存储。 -
作为一个很好的起点(实际上它几乎提供了你所需要的一切),阅读这篇Oracle的文章。它详细解释了SCP的工作原理。
-
如果你想看一个类似语言的很好的示例,可以参考这个Python实现的SCP。
这些应该能帮助你入门。祝好运!
英文:
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.
-
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, andscp -f
being 'from'/source mode. Essentiallyscp -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. -
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.
-
For a good example in a similar language, see this python implementation of scp
That should set you on your way. Cheers
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论