Skip to content

Instantly share code, notes, and snippets.

@cygnetix
Forked from jedy/go_scp.go
Created October 7, 2015 05:34
Show Gist options
  • Select an option

  • Save cygnetix/624a7c433858acbcd6bf to your computer and use it in GitHub Desktop.

Select an option

Save cygnetix/624a7c433858acbcd6bf to your computer and use it in GitHub Desktop.

Revisions

  1. @jedy jedy revised this gist Apr 10, 2015. 1 changed file with 13 additions and 34 deletions.
    47 changes: 13 additions & 34 deletions go_scp.go
    Original file line number Diff line number Diff line change
    @@ -2,44 +2,19 @@
    package main

    import (
    "code.google.com/p/go.crypto/ssh"
    "crypto"
    "crypto/rsa"
    "crypto/x509"
    "encoding/pem"
    "fmt"
    "io"

    "golang.org/x/crypto/ssh"
    )

    const privateKey = `content of id_rsa`

    type keychain struct {
    key *rsa.PrivateKey
    }

    func (k *keychain) Key(i int) (interface{}, error) {
    if i != 0 {
    return nil, nil
    }
    return &k.key.PublicKey, nil
    }

    func (k *keychain) Sign(i int, rand io.Reader, data []byte) (sig []byte, err error) {
    hashFunc := crypto.SHA1
    h := hashFunc.New()
    h.Write(data)
    digest := h.Sum(nil)
    return rsa.SignPKCS1v15(rand, k.key, hashFunc, digest)
    }

    func main() {
    block, _ := pem.Decode([]byte(privateKey))
    rsakey, _ := x509.ParsePKCS1PrivateKey(block.Bytes)
    clientKey := &keychain{rsakey}
    signer, _ := ssh.ParsePrivateKey([]byte(privateKey))
    clientConfig := &ssh.ClientConfig{
    User: "wuhao",
    Auth: []ssh.ClientAuth{
    ssh.ClientAuthKeyring(clientKey),
    User: "jedy",
    Auth: []ssh.AuthMethod{
    ssh.PublicKeys(signer),
    },
    }
    client, err := ssh.Dial("tcp", "127.0.0.1:22", clientConfig)
    @@ -55,11 +30,15 @@ func main() {
    w, _ := session.StdinPipe()
    defer w.Close()
    content := "123456789\n"
    fmt.Fprintln(w, "C0644", len(content), "testfile")
    fmt.Fprintln(w, "D0755", 0, "testdir") // mkdir
    fmt.Fprintln(w, "C0644", len(content), "testfile1")
    fmt.Fprint(w, content)
    fmt.Fprint(w, "\x00") // transfer end with \x00
    fmt.Fprintln(w, "C0644", len(content), "testfile2")
    fmt.Fprint(w, content)
    fmt.Fprint(w, "\x00") // 传输以\x00结束
    fmt.Fprint(w, "\x00")
    }()
    if err := session.Run("/usr/bin/scp -qrt ./"); err != nil {
    if err := session.Run("/usr/bin/scp -tr ./"); err != nil {
    panic("Failed to run: " + err.Error())
    }
    }
  2. @jedy jedy revised this gist Jul 25, 2013. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion go_scp.go
    Original file line number Diff line number Diff line change
    @@ -21,7 +21,7 @@ func (k *keychain) Key(i int) (interface{}, error) {
    if i != 0 {
    return nil, nil
    }
    return k.key.PublicKey, nil
    return &k.key.PublicKey, nil
    }

    func (k *keychain) Sign(i int, rand io.Reader, data []byte) (sig []byte, err error) {
  3. @jedy jedy revised this gist Jul 23, 2013. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion go_scp.go
    Original file line number Diff line number Diff line change
    @@ -11,7 +11,7 @@ import (
    "io"
    )

    const privateKey = ``
    const privateKey = `content of id_rsa`

    type keychain struct {
    key *rsa.PrivateKey
  4. @jedy jedy revised this gist Aug 25, 2012. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion go_scp.go
    Original file line number Diff line number Diff line change
    @@ -57,7 +57,7 @@ func main() {
    content := "123456789\n"
    fmt.Fprintln(w, "C0644", len(content), "testfile")
    fmt.Fprint(w, content)
    fmt.Fprint(w, "\x00") // 文件结束时加\0
    fmt.Fprint(w, "\x00") // 传输以\x00结束
    }()
    if err := session.Run("/usr/bin/scp -qrt ./"); err != nil {
    panic("Failed to run: " + err.Error())
  5. @jedy jedy revised this gist Aug 25, 2012. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion go_scp.go
    Original file line number Diff line number Diff line change
    @@ -21,7 +21,7 @@ func (k *keychain) Key(i int) (interface{}, error) {
    if i != 0 {
    return nil, nil
    }
    return &k.key.PublicKey, nil
    return k.key.PublicKey, nil
    }

    func (k *keychain) Sign(i int, rand io.Reader, data []byte) (sig []byte, err error) {
  6. @jedy jedy revised this gist Aug 15, 2012. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion go_scp.go
    Original file line number Diff line number Diff line change
    @@ -57,7 +57,7 @@ func main() {
    content := "123456789\n"
    fmt.Fprintln(w, "C0644", len(content), "testfile")
    fmt.Fprint(w, content)
    fmt.Fprint(w, "\x00")
    fmt.Fprint(w, "\x00") // 文件结束时加\0
    }()
    if err := session.Run("/usr/bin/scp -qrt ./"); err != nil {
    panic("Failed to run: " + err.Error())
  7. @jedy jedy created this gist Aug 15, 2012.
    65 changes: 65 additions & 0 deletions go_scp.go
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,65 @@
    // https://blogs.oracle.com/janp/entry/how_the_scp_protocol_works
    package main

    import (
    "code.google.com/p/go.crypto/ssh"
    "crypto"
    "crypto/rsa"
    "crypto/x509"
    "encoding/pem"
    "fmt"
    "io"
    )

    const privateKey = ``

    type keychain struct {
    key *rsa.PrivateKey
    }

    func (k *keychain) Key(i int) (interface{}, error) {
    if i != 0 {
    return nil, nil
    }
    return &k.key.PublicKey, nil
    }

    func (k *keychain) Sign(i int, rand io.Reader, data []byte) (sig []byte, err error) {
    hashFunc := crypto.SHA1
    h := hashFunc.New()
    h.Write(data)
    digest := h.Sum(nil)
    return rsa.SignPKCS1v15(rand, k.key, hashFunc, digest)
    }

    func main() {
    block, _ := pem.Decode([]byte(privateKey))
    rsakey, _ := x509.ParsePKCS1PrivateKey(block.Bytes)
    clientKey := &keychain{rsakey}
    clientConfig := &ssh.ClientConfig{
    User: "wuhao",
    Auth: []ssh.ClientAuth{
    ssh.ClientAuthKeyring(clientKey),
    },
    }
    client, err := ssh.Dial("tcp", "127.0.0.1:22", clientConfig)
    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())
    }
    }