go 实现windows scp 文件到 linux 服务器

代码如下

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") 
}

猜你喜欢

转载自my.oschina.net/u/4038454/blog/2963445
今日推荐