国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Golang > 正文

go語言使用scp的方法實例分析

2020-04-01 19:15:51
字體:
供稿:網(wǎng)友
這篇文章主要介紹了go語言使用scp的方法,實例分析了go語言調(diào)用scp命令的使用技巧,具有一定參考借鑒價值,需要的朋友可以參考下
 

本文實例講述了go語言使用scp的方法。分享給大家供大家參考。具體如下:

 

復(fù)制代碼代碼如下:
package main
import (
    "code.google.com/p/go.crypto/ssh"
    "crypto"
    "crypto/rsa"
    "crypto/x509"
    "encoding/pem"
    "fmt"
    "io"
)
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}
    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") // 傳輸以/x00結(jié)束
    }()
    if err := session.Run("/usr/bin/scp -qrt ./"); err != nil {
        panic("Failed to run: " + err.Error())
    }
}

 

希望本文所述對大家的Go語言程序設(shè)計有所幫助。


發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 揭西县| 平塘县| 榆社县| 玉林市| 霍林郭勒市| 鄂托克前旗| 共和县| 星子县| 嵊州市| 长沙县| 安丘市| 平乐县| 青龙| 巨鹿县| 秦安县| 兰坪| 德令哈市| 商水县| 乌拉特前旗| 枣阳市| 阳春市| 赤峰市| 洞口县| 元氏县| 濮阳县| 当雄县| 穆棱市| 闻喜县| 新丰县| 济宁市| 信宜市| 安远县| 莆田市| 古蔺县| 桂平市| 延边| 承德市| 五寨县| 阜新市| 荥阳市| 元谋县|