Go language Using SCP Method Example Analysis _golang

Source: Internet
Author: User
Tags rand ssh

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.