SCP Client in Go

2019-06-13 08:13发布

问题:

I was struggling with and ssh client for golang 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 scp client for go and have two issues.

  1. I run it and get this:

  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:

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())
    }

}


标签: ssh go scp