Golang的SCP范例[关闭]

我花了3个小时的时间在Go中搜索SCP实施</ p>

找不到任何可用的lib或示例。</ p>

此要点无效。 它有问题,但在某些答案中列为有效代码。
https://gist.github.com/jedy / 3357393 </ p>
</ div>

展开原文

原文

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

droxy80248
droxy80248 您可能会得到很多反对意见。说出什么不起作用,说出您期望的内容,将代码复制到问题中(不要在此处放置链接),您可能会有更好的机会获得有用的答案。
6 年多之前 回复

4个回答



我还没有尝试使用它,但是在godoc上搜索SCP使我 scp-go http://godoc.org/github.com/laher/scp-go/scp </ p>
</ div>

展开原文

原文

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

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.



这是最高级的

除了常规的CRUD操作外,它还支持批量上传,符号链接和修改时间保存。</ p>

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

超时MS:= 15000
服务,错误:= scp.NewStorager(“ 127.0.0.1 :22“,timeoutMS,配置)
,如果错误!= nil {
log.Fatal(err)
}
ctx:= context.Background()
位置:=” / tmp / myfile“
err = service.Upload(ctx,location,0644,[] byte(“ somedata”))
如果err!= nil {
log.Fatal(err)
}
读者,err:= service.Download (ctx,location)
if err!= nil {
log.Fatal(err)
}

</ code> </ pre>
</ div>

展开原文

原文

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

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问