为了账号安全,请及时绑定邮箱和手机立即绑定

go-scp 库不工作但 scp 工作正常

go-scp 库不工作但 scp 工作正常

Go
慕标5832272 2022-11-23 19:58:20
我正在使用 go-scp 并尝试复制到 solarwinds 服务器(windows 服务器)并等待超时错误,而我尝试命令行 scp 它工作正常。我还发现在 go-scp 库err := a.Session.Run(fmt.Sprintf("%s -qt %q", a.RemoteBinary, remotePat) 中的CopyPassThru函数中删除 -q 选项后,没有等待超时错误,但文件在远程服务器上为空我无法通过命令行 SSH 到 solarwinds 服务器。代码如下所示package mainimport (    "fmt"    scp "github.com/bramvdbogaerde/go-scp"    "golang.org/x/crypto/ssh"    "os"    "strings"    "time")func main() {    // Use SSH key authentication from the auth package    // we ignore the host key in this example, please change this if you use this library    // create ssh client config    var authParam ssh.AuthMethod    authParam = ssh.Password("1234")    clientConfig := &ssh.ClientConfig{        User: "admin",        Auth: []ssh.AuthMethod{            authParam,        },        HostKeyCallback: ssh.InsecureIgnoreHostKey(),        Timeout:time.Minute,    }    // For other authentication methods see ssh.ClientConfig and ssh.AuthMethod    // Create a new SCP client    client := scp.NewClient("10.154.92.32:22", clientConfig)    // Connect to the remote server    err := client.Connect()    if err != nil {        fmt.Println("Couldn't establish a connection to the remote server ", err)        return    }    // Close client connection after the file has been copied    defer client.Close()    // Finally, copy the file over    // Usage: CopyFile(fileReader, remotePath, permission)    fileString := "testing \n"     myReader := strings.NewReader(fileString)    err = client.CopyFile(myReader, "/test", "0777")    if err != nil {        fmt.Println("Error while copying file ", err)    }}
查看完整描述

1 回答

?
呼如林

TA贡献1798条经验 获得超3个赞

对于对该scp包有问题的任何人(我也有问题),这是一种cat用于传输单个文件的解决方法。它只使用ssh包。


这个想法是使用不带参数的 cat 从标准输入读取。在我们的会话对象中,我们提供本地文件作为标准输入。然后我们将 cat 的输出通过管道>传输到所需的文件。


反向方式类似,这次我们拦截会话对象的标准输出。我们 cat 远程文件并将会话的标准输出复制到我们的本地文件。


这是代码:


package main


import (

    "bytes"

    "errors"

    "os"


    "golang.org/x/crypto/ssh"

)


func main() {

    config := &ssh.ClientConfig{

        HostKeyCallback: ssh.InsecureIgnoreHostKey(),

        User:            "user",

        Auth:            []ssh.AuthMethod{ssh.Password("password")},

    }

    client, err := ssh.Dial("tcp", "10.0.0.1:22", config)

    if err != nil {

        panic(err)

    }

    defer client.Close()


    err = setFile(client, "local/file", "remote/file")

    if err != nil {

        panic(err)

    }


    err = getFile(client, "remote/file", "local/file")

    if err != nil {

        panic(err)

    }

}


func setFile(client *ssh.Client, from, to string) error {

    f, err := os.Open(from)

    if err != nil {

        return err

    }

    defer f.Close()


    session, err := client.NewSession()

    if err != nil {

        return err

    }

    defer session.Close()


    session.Stdin = f

    var stderr bytes.Buffer

    session.Stderr = &stderr

    err = session.Run("cat > '" + to + "'")

    if err != nil && stderr.Len() > 0 {

        err = errors.New(err.Error() + ": " + string(stderr.Bytes()))

    }

    return err

}


func getFile(client *ssh.Client, from, to string) error {

    f, err := os.Create(to)

    if err != nil {

        return err

    }

    defer f.Close()


    session, err := client.NewSession()

    if err != nil {

        return err

    }

    defer session.Close()


    session.Stdout = f

    var stderr bytes.Buffer

    session.Stderr = &stderr

    err = session.Run("cat '" + from + "'")

    if err != nil && stderr.Len() > 0 {

        err = errors.New(err.Error() + ": " + string(stderr.Bytes()))

    }

    return err

}


查看完整回答
反对 回复 2022-11-23
  • 1 回答
  • 0 关注
  • 89 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信