linux - go-scp 库无法工作,但 scp 工作正常

标签 linux go ssh scp

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

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

我无法通过命令行 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)
    }
}

最佳答案

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

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

相反的方式类似,这次我们拦截 session 对象的标准输出。我们捕获远程文件并将 session 的标准输出复制到本地文件。

这是代码:

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
}

关于linux - go-scp 库无法工作,但 scp 工作正常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71599643/

相关文章:

http - 解密后的反向代理服务文件

go - "3 state"Go 中的命令行参数

windows - 如何在 Windows 上为在 Linux 上创建的 git 使用 ssh key ?

ssh - VSCode 远程 SSH 连接失败

linux - 我想在 linux 上删除多行文本

linux - lua 和 env lua 的区别?

linux - 有没有办法使 linux CLI IO 重定向持久化?

amazon-web-services - 为什么 AWS elastic beanstalk 无法构建我的应用程序?

hadoop - 试图让 Hadoop 在伪分布式模式下工作 : connection refused and other errors

linux - 如何从用户空间向 alsa 注册虚拟卡?