go 實現windows scp 檔案到 linux 伺服器
阿新 • • 發佈:2018-11-30
程式碼如下
package main import ( "net" "os" "github.com/bramvdbogaerde/go-scp" "golang.org/x/crypto/ssh" ) func main() { //clientConfig, _ := auth.PrivateKey("ubuntu", "", ssh.InsecureIgnoreHostKey()) clientConfig := &ssh.ClientConfig{ User: "ubuntu", Auth: []ssh.AuthMethod{ssh.Password("PASSWORD")}, HostKeyCallback: func(hostname string, remote net.Addr, key ssh.PublicKey) error { return nil }, } client := scp.NewClient("IP:22", clientConfig) f, _ := os.Open("C:\\a\\b.txt") //windows 檔案路徑 client.Connect() defer client.Close() defer f.Close() client.CopyFile(f, "/home/ubuntu/", "0655") }