在Golang中的SCP示例

huangapple go评论121阅读模式
英文:

SCP example in Golang

问题

我花了3个小时在Google上搜索Go语言中可用的SCP实现,但是我找不到任何可用的库或示例。这个gist不起作用,它有些错误,但在一些答案中被列为可用的代码。https://gist.github.com/jedy/3357393

英文:

I spent 3 hours googling for working SCP implementation in Go

I can't find any working lib or example.

This gist is not working. It is buggy but listed in some answers as working code.
https://gist.github.com/jedy/3357393

答案1

得分: 1

这是Go语言中最先进的SCP客户端实现。

除了常规的CRUD操作,它还支持批量上传、符号链接和修改时间保留。

var config *ssh.ClientConfig
//加载配置...

timeoutMS := 15000
service, err := scp.NewStorager("127.0.0.1:22", timeoutMS, config)
if err != nil {
    log.Fatal(err)
}
ctx := context.Background()
location := "/tmp/myfile"
err = service.Upload(ctx, location, 0644, []byte("somedata"))
if err != nil {
    log.Fatal(err)
}
reader, err := service.Download(ctx, location)
if err != nil {
    log.Fatal(err)
}
英文:

Here is the most advanced SCP client implementation in go.

Beside regular CRUD operations, it also supports batch upload, symbolic links and modification time preservation.


	var config *ssh.ClientConfig
	//load config ...

	timeoutMS := 15000
	service, err := scp.NewStorager("127.0.0.1:22", timeoutMS, config)
	if err != nil {
		log.Fatal(err)
	}
	ctx := context.Background()
	location := "/tmp/myfile"
	err = service.Upload(ctx,  location, 0644, []byte("somedata"))
	if err != nil {
		log.Fatal(err)
	}
	reader, err := service.Download(ctx,  location)
	if err != nil {
		log.Fatal(err)
	}

答案2

得分: 0

我还没有尝试使用它,但在godoc上搜索SCP给我找到了scp-go。链接:http://godoc.org/github.com/laher/scp-go/scp

英文:

I haven't tried using it, but a search for SCP on godoc got me scp-go. http://godoc.org/github.com/laher/scp-go/scp

答案3

得分: 0

你可以使用简单的https://github.com/tmc/scp代码片段将本地文件复制到远程机器上使用scp的方法如下:

package main

import (
	"io/ioutil"
	"net"

	"github.com/tmc/scp"
	"golang.org/x/crypto/ssh"
)

func getKeyFile() (key ssh.Signer, err error) {
	//usr, _ := user.Current()
	file := "你的密钥文件路径(.pem)"
	buf, err := ioutil.ReadFile(file)
	if err != nil {
		return
	}
	key, err = ssh.ParsePrivateKey(buf)
	if err != nil {
		return
	}
	return
}

func main() {
	key, err := getKeyFile()
	if err != nil {
		panic(err)
	}

	// 定义客户端配置:
	config := &ssh.ClientConfig{
		User: "root",
		Auth: []ssh.AuthMethod{
			ssh.PublicKeys(key),
		},
		HostKeyCallback: func(hostname string, remote net.Addr, key ssh.PublicKey) error {
			return nil
		},
	}
	client, err := ssh.Dial("tcp", "<远程IP>:22", config)
	if err != nil {
		panic("Failed to dial: " + err.Error())
	}

	session, err := client.NewSession()
	if err != nil {
		panic("Failed to create session: " + err.Error())
	}
	err = scp.CopyPath("本地文件路径", "远程路径", session)
	if err != nil {
		panic("Failed to Copy: " + err.Error())
	}
	defer session.Close()

	Hope it helps.
}

希望对你有所帮助。

英文:

you can use a simple
https://github.com/tmc/scp

Code snippet for copying a local file to remote machine using scp is below

package main
import (
&quot;io/ioutil&quot;
&quot;net&quot;
&quot;github.com/tmc/scp&quot;
&quot;golang.org/x/crypto/ssh&quot;
)
func getKeyFile() (key ssh.Signer, err error) {
//usr, _ := user.Current()
file := &quot;Path to your key file(.pem)&quot;
buf, err := ioutil.ReadFile(file)
if err != nil {
return
}
key, err = ssh.ParsePrivateKey(buf)
if err != nil {
return
}
return
}
func main() {
key, err := getKeyFile()
if err != nil {
panic(err)
}
// Define the Client Config as :
config := &amp;ssh.ClientConfig{
User: &quot;root&quot;,
Auth: []ssh.AuthMethod{
ssh.PublicKeys(key),
},
HostKeyCallback: func(hostname string, remote net.Addr, key ssh.PublicKey) error {
return nil
},
}
client, err := ssh.Dial(&quot;tcp&quot;, &quot;&lt;remote ip&gt;:22&quot;, config)
if err != nil {
panic(&quot;Failed to dial: &quot; + err.Error())
}
session, err := client.NewSession()
if err != nil {
panic(&quot;Failed to create session: &quot; + err.Error())
}
err = scp.CopyPath(&quot;local file path&quot;, &quot;remote path&quot;, session)
if err != nil {
panic(&quot;Failed to Copy: &quot; + err.Error())
}
defer session.Close() 

Hope it helps.

huangapple
  • 本文由 发表于 2014年4月13日 22:25:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/23043938.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定