Golang implementing a cross-platform SCP batch bulk file

Source: Internet
Author: User
Tags sprintf scp command
This is a creation in Article, where the information may have evolved or changed.
Package Mainimport ("Bufio" "Flag" "FMT" "IO" "OS" "Path/filepath" "Strings" "Github.com/czxichen/autowork/tools/split" "Golang.org/x/crypto/ssh") var (passwd = flag. String ("P", "", "-p passwd Specifies the password.") user = flag. String ("U", "root", "-u root" Specify login user. ") CFG = flag. String ("C", "ServerList", "-C serverlist Specify ServerList") Ip_port = flag. String ("I", "", "-I ip:port specifies the IP port of the target machine and cannot be used in conjunction with-C.") Dpath = flag. String ("D", "", "-d/tmp/20160531.zip specifies the path to be sent to, cannot be empty.") spath = flag. String ("s", "", "-s 20160531.zip specifies the path of the file to be sent, cannot be empty.")) Func main () {flag. Parse () if *dpath = = "" | | *spath = = "" {flag. Printdefaults () OS. Exit (1)}file, err: = OS. Open (*spath) if err! = Nil {fmt. Println ("Open file failed:", err) OS. Exit (1)}info, _: = File.stat () defer file.close () if *ip_port! = "" && *passwd! = "" {Client, err: = Dail (*user, *pa SSWD, *ip_port) if err! = Nil {fmt. Printf ("connection%s failed. \ n", err) OS. Exit (1)}scp (Client, File, info. Size (), *dpath) return}var list [][]stringok: = (*passwd! = "" "&& *ip_port = =" ") List = Config (*cfg, OK) If Len (list) <= 0 {fmt. Println ("ServerList cannot be empty.") Os. Exit (1)}for _, V: = Range List {If ok {*ip_port = v[0]} else {*user = V[0]*PASSWD = V[1]*ip_port = V[2]}client, err: = Dai L (*user, *passwd, *ip_port) if err! = Nil {fmt. Printf ("connection%s failed. \ n", err) CONTINUE}SCP (Client, File, info.) Size (), *dpath)}}func dail (user, password, ip_port string) (*ssh. Client, error) {PassWd: = []ssh. Authmethod{ssh. Password (Password)}conf: = ssh. Clientconfig{user:user, Auth:passwd}return ssh. Dial ("TCP", Ip_port, &conf)}func SCP (Client *ssh. Client, File io. Reader, size int64, path string) {filename: = filepath. Base (path) dirname: = Strings. Replace (filepath. Dir (path), "\ \", "/",-1) Defer Client.close () session, Err: = Client.newsession () if err! = Nil {fmt. PRINTLN ("Create Session failed:", err) Return}go func () {W, _: = Session. Stdinpipe () fmt. Fprintln (W, "C0644", size, filename) io. Copyn (W, File, size) fmt. Fprint (W, "\x00") W.close ()} () If err: = Session. Run (FMT. Sprintf ("/usr/bin/scp-qrt%s", dirname)); Err! = Nil {fmt.Println ("Execute SCP Command failed:", err) return} else {fmt. Printf ("%s sent successfully. \ n", Client.remoteaddr ()) session. Close ()}if session, Err = Client.newsession (); Err = = Nil {defer session. Close () buf, err: = Session. Output (FMT. Sprintf ("/usr/bin/md5sum%s", path)) If err! = Nil {fmt. PRINTLN ("Check MD5 failed:", err) return}fmt. Printf ("md5:\n%s\n" of "%s", Client.remoteaddr (), String (buf))}}func config (path string, OK bool) (list [][]string) {File, er R: = OS. Open (PATH) if err! = Nil {fmt. Printf ("Open config file failed:%s\n", err) OS. Exit (1)}defer file.close () buf: = Bufio. Newreader (File) for {line, _, Err: = buf. ReadLine () if err! = Nil {break}str: = strings. Trimspace (String) STRs: = Split. Split (str) If OK {if Len (strs)! = 1 {Continue}} else {if Len (strs)! = 3 {continue}}list = append (list, STRs)}return}

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.