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

我正在使用 go-scp 并尝试复制到 solarwinds 服务器(windows 服务器)并等待超时错误,而我尝试命令行 scp 它工作正常。


我还发现在 go-scp 库err := a.Session.Run(fmt.Sprintf("%s -qt %q", a.RemoteBinary, remotePat) 中的CopyPassThru函数中删除 -q 选项后,没有等待超时错误,但文件在远程服务器上为空


我无法通过命令行 SSH 到 solarwinds 服务器。


代码如下所示


package main


import (

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

    }

}


慕标5832272
浏览 84回答 1
1回答

呼如林

对于对该scp包有问题的任何人(我也有问题),这是一种cat用于传输单个文件的解决方法。它只使用ssh包。这个想法是使用不带参数的 cat 从标准输入读取。在我们的会话对象中,我们提供本地文件作为标准输入。然后我们将 cat 的输出通过管道>传输到所需的文件。反向方式类似,这次我们拦截会话对象的标准输出。我们 cat 远程文件并将会话的标准输出复制到我们的本地文件。这是代码:package mainimport (    "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}
打开App,查看更多内容
随时随地看视频慕课网APP