go語言使用scp的方法實(shí)例分析
更新時間:2015年03月13日 09:09:44 作者:feiwen
這篇文章主要介紹了go語言使用scp的方法,實(shí)例分析了go語言調(diào)用scp命令的使用技巧,具有一定參考借鑒價值,需要的朋友可以參考下
本文實(shí)例講述了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())
}
}
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è)計有所幫助。
相關(guān)文章
Go?結(jié)構(gòu)體序列化的實(shí)現(xiàn)
本文主要介紹了Go?結(jié)構(gòu)體序列化的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-01-01Go 結(jié)構(gòu)體、數(shù)組、字典和 json 字符串的相互轉(zhuǎn)換方法
今天小編就為大家分享一篇Go 結(jié)構(gòu)體、數(shù)組、字典和 json 字符串的相互轉(zhuǎn)換方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-08-08Go語言中?Print?Printf和Println?的區(qū)別解析
這篇文章主要介紹了Go語言中?Print?Printf和Println?的區(qū)別,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-03-03Go-Web框架中AOP方案的實(shí)現(xiàn)方式
本文主要介紹了Go-Web框架中AOP方案的實(shí)現(xiàn)方式,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-06-06