英文:
SCP Client in Go
问题
我正在为你翻译以下内容:
我一直在为golang寻找一个ssh客户端,并被告知freesshd服务器的加密方式与Go的ssh客户端不兼容,所以我安装了另一个(PowerShell Server),并成功连接到了服务器。
但我的问题还没有解决,因为我现在需要通过scp将文件从本地传输到远程服务器。我被引导到了这个scp客户端 for go,并遇到了两个问题。
-
我运行它后得到了这个错误:
-
我应该在哪里或如何访问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.
-
I run it and get this:
-
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 (
"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())
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论