SoFunction
Updated on 2025-03-04

Example analysis of the method using scp in go language

package main
import (
    "/p//ssh"
    "crypto"
    "crypto/rsa"
    "crypto/x509"
    "encoding/pem"
    "fmt"
    "io"
)
const privateKey = `content of id_rsa`
type keychain struct {
    key *
}
func (k *keychain) Key(i int) (interface{}, error) {
    if i != 0 {
        return nil, nil
    }
    return &, nil
}
func (k *keychain) Sign(i int, rand , data []byte) (sig []byte, err error) {
    hashFunc := crypto.SHA1
    h := ()
    (data)
    digest := (nil)
    return rsa.SignPKCS1v15(rand, , hashFunc, digest)
}
func main() {
    block, _ := ([]byte(privateKey))
    rsakey, _ := x509.ParsePKCS1PrivateKey()
    clientKey := &keychain{rsakey}
    clientConfig := &{
        User: "wuhao",
        Auth: []{
            (clientKey),
        },
    }
    client, err := ("tcp", "127.0.0.1:22", clientConfig)
    if err != nil {
        panic("Failed to dial: " + ())
    }
    session, err := ()
    if err != nil {
        panic("Failed to create session: " + ())
    }
    defer ()
    go func() {
        w, _ := ()
        defer ()
        content := "123456789\n"
        (w, "C0644", len(content), "testfile")
        (w, content)
(w, "\x00") // The transmission ends with \x00
    }()
    if err := ("/usr/bin/scp -qrt ./"); err != nil {
        panic("Failed to run: " + ())
    }
}