我正在使用go并试图复制到solarwinds服务器(windows服务器)并获得等待超时错误,而我尝试了命令行scp,它工作得很好。
我还发现,在删除了go-scp库中的-q函数行err := a.Session.Run(fmt.Sprintf("%s -qt %q", a.RemoteBinary, remotePat)中的CopyPassThru选项后,没有出现等待超时错误,但是远程服务器上的文件是空的。
我无法通过命令行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)
}
}发布于 2022-03-24 09:26:30
对于任何有问题的scp包(我也有问题),这是一个工作,使用cat来传输单个文件。它只使用ssh包。
这样做的目的是使用没有参数的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
}https://stackoverflow.com/questions/71599643
复制相似问题