The example in this article describes how the go language uses the SCP. Share to everyone for your reference. Specifically as follows:
Copy Code code as follows:
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 to 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")//transmission ends with \x00
}()
If err: = Session. Run ("/usr/bin/scp-qrt./"); Err!= Nil {
Panic ("Failed to run:" + Err.) Error ())
}
}
I hope this article will help you with your go language program.