英文:
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 (
"io/ioutil"
"net"
"github.com/tmc/scp"
"golang.org/x/crypto/ssh"
)
func getKeyFile() (key ssh.Signer, err error) {
//usr, _ := user.Current()
file := "Path to your key 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)
}
// Define the Client Config as :
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", "<remote 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("local file path", "remote path", session)
if err != nil {
panic("Failed to Copy: " + err.Error())
}
defer session.Close()
Hope it helps.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论