Go中的SCP客户端

huangapple go评论156阅读模式
英文:

SCP Client in Go

问题

我正在为你翻译以下内容:

我一直在为golang寻找一个ssh客户端,并被告知freesshd服务器的加密方式与Go的ssh客户端不兼容,所以我安装了另一个(PowerShell Server),并成功连接到了服务器。

但我的问题还没有解决,因为我现在需要通过scp将文件从本地传输到远程服务器。我被引导到了这个scp客户端 for go,并遇到了两个问题。

  1. 我运行它后得到了这个错误:
    Go中的SCP客户端

  2. 我应该在哪里或如何访问id_rsa文件的内容以获取privateKey?我刚刚进入我的.ssh文件夹,看到了一个github_rsa文件,并使用了那个私钥。我确定这不是正确的私钥,但我不应该看到某种错误或无效的私钥,而不是上面的结果吗?

英文:

I was struggling with and <a href="https://gist.github.com/linuz-ly/4148437">ssh client for golang</a> but was told that the ciphers for the freesshd server was incompatible with the ssh client for Go, so I just installed another one (PowerShell Server) and I can successfully connect to the server.

My problem is not over because I now need to transfer files from local to remote, and this can only be done through scp. I was directed to this <a href="https://gist.github.com/jedy/3357393">scp client for go</a> and have two issues.

  1. I run it and get this:
    Go中的SCP客户端

  2. Where or how can I access the contents of id_rsa needed for privateKey? I just went in my .ssh folder and saw a github_rsa and used that private key, I'm sure that this is not the correct one to use but wouldn't I see some kind of error or invalid private key and not the result above?

答案1

得分: 3

你被引导到的代码有问题。示例还使用了公钥身份验证,这不一定是你唯一的选择。如果你可以允许使用密码身份验证,可以让事情变得更容易一些。

我刚刚通过修改你使用的示例来进行了上传:

package main

import (
	"code.google.com/p/go.crypto/ssh"
	"fmt"
)

type password string

func (p password) Password(_ string) (string, error) {
	return string(p), nil
}

func main() {

	// Dial code is taken from the ssh package example
	config := &ssh.ClientConfig{
		User: "username",
		Auth: []ssh.ClientAuth{
			ssh.ClientAuthPassword(password("password")),
		},
	}
	client, err := ssh.Dial("tcp", "127.0.0.1:22", config)
	if err != nil {
		panic("Failed to dial: " + err.Error())
	}

	session, err := client.NewSession()
	if err != nil {
		panic("Failed to create session: " + err.Error())
	}
	defer session.Close()

	
	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")
	}()
	if err := session.Run("/usr/bin/scp -qrt ./"); err != nil {
		panic("Failed to run: " + err.Error())
	}
	
}
英文:

The code you were directed to is broken. The example also uses the public key authentication, which is not necessarily your only option. If you can allow password authentication instead, you can make it a bit easier for yourself.

I just made an upload myself by modifying the example you used:

package main

import (
	&quot;code.google.com/p/go.crypto/ssh&quot;
	&quot;fmt&quot;
)

type password string

func (p password) Password(_ string) (string, error) {
	return string(p), nil
}

func main() {

	// Dial code is taken from the ssh package example
	config := &amp;ssh.ClientConfig{
		User: &quot;username&quot;,
		Auth: []ssh.ClientAuth{
			ssh.ClientAuthPassword(password(&quot;password&quot;)),
		},
	}
	client, err := ssh.Dial(&quot;tcp&quot;, &quot;127.0.0.1:22&quot;, config)
	if err != nil {
		panic(&quot;Failed to dial: &quot; + err.Error())
	}

	session, err := client.NewSession()
	if err != nil {
		panic(&quot;Failed to create session: &quot; + err.Error())
	}
	defer session.Close()

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

huangapple
  • 本文由 发表于 2013年9月26日 15:28:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/19021964.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定