本文实例讲述了go语言使用scp的方法。分享给大家供大家参考。具体如下:
复制代码 代码如下: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结束
}()
if err := session.Run("/usr/bin/scp -qrt ./"); err != nil {
panic("Failed to run: " + err.Error())
}
}
希望本文所述对大家的Go语言程序设计有所帮助。
go语言,使用,scp,方法
更新动态
- 物华弥新迷踪盘第15关怎么过 迷踪盘第15关通关方法
- win7怎么更换激活码,win7更改激活码
- 电脑怎么安装安卓系统不卡的,电脑怎么安装安卓系统软件
- 《天国拯救2》实体版已经在亚马逊等网站上开启预购
- Vandal爆料称 已获悉了Nintendo Switch 2的相关信息
- 打卡爱好者福音 收藏类游戏《Check in》登陆Steam
- 谈诗玲.2019-月情思曲【豪记】【WAV+CUE】
- 陈淑桦-试音陈淑桦24K金碟-正版原抓WAV+CUE.
- 韩宝仪《痴心的小妹》(24K黄金首版)WAV
- 玩家称《辐射4》次世代会让PS5过热:其它游戏就没事
- 山西午睡文化已经不分物种了:家里的猫 路上的狗
- 《辐射》剧集开局王炸!亚马逊平台开局最佳剧集
- 杨采妮.1997-离别之前新曲+精选(告别专辑)【EMI百代】【WAV+CUE】
- 石凯.2024-数星一整夜(EP)【宇宙星船】【FLAC分轨】
- 群星.1993-开心跳不停【华纳】【WAV+CUE】