go语言使用scp的方法实例分析
2015-03-13 09:09
1376 查看
复制代码 代码如下:
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())
}
}
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语言日志记录库简单使用方法实例分析
- Go语言中常量定义方法实例分析
- 【Go语言】LiteIDE使用的个人使用方法
- Go语言中struct的匿名属性特征实例分析
- Go语言中的range用法实例分析
- Go语言排序与接口实例分析
- Go语言共享内存读写实例分析
- Go语言开发-关于LiteIDE使用第三库时没有自动提示的解决方法
- Go语言中的switch用法实例分析
- Go语言里的结构体文法实例分析
- Go语言map字典用法实例分析
- GO语言类型转换和类型断言实例分析
- Go语言使用组合的方式实现多继承的方法
- Go语言中的方法定义用法分析
- linux下scp命令的使用方法详解实例
- jQuery中DOM树操作之使用反向插入方法实例分析
- jsp中Action使用session方法实例分析
- jsp中Action使用session方法实例分析
- Go语言中的指针运算实例分析
- Go语言中错误处理实例分析