Package Toolsimport ("FMT" "Golang.org/x/crypto/ssh" "golang.org/x/crypto/ssh/agent" "IO" "NET" "OS" "SFTP") Func FTP ( User, password, IP, port string, file_list []string) {var auths []ssh. Authmethodif Aconn, err: = Net. Dial ("Unix", Os. Getenv ("Ssh_auth_sock")); Err = = Nil {auths = append (auths, ssh. Publickeyscallback (agent. Newclient (Aconn). Signers))}auths = append (auths, ssh. Password (Password)) Config: = ssh. CLIENTCONFIG{USER:USER,AUTH:AUTHS,}ADDR: = Fmt. Sprintf ("%s:%s", IP, Port) conn, err: = ssh. Dial ("TCP", addr, &config) if err! = Nil {fmt. PRINTLN (ERR) Return}defer Conn. Close () c, err: = Sftp.newclient (conn, Sftp.maxpacket (5E9)) if err! = Nil {fmt. PRINTLN (Err) Return}for _, K: = Range file_list {FS, err: = C.open (filename)//logic writes itself if err! = Nil {fmt. PRINTLN (Err) Continue}info, _: = fs. Stat () File, _: = OS. OpenFile (k, OS. O_rdwr|os. O_create|os. O_trunc, 0644) io. Copy (File, Io. Limitreader (FS, info. Size ()))}}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Go1.5 using SFTP to download files