I am using go-scp and trying to copy to solarwinds server(windows server) and getting waiting timed out error whereas I tried command line scp it works fine.
Also I found out that after removing -q option in line err := a.Session.Run(fmt.Sprintf("%s -qt %q", a.RemoteBinary, remotePat)
in CopyPassThru
function in go-scp library, there was no waiting timed out error but the file was empty at the remote server
I am unable to SSH to solarwinds server through command line.
Code snipped as below
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)
}
}
For anybody having problems with that scp
package (I had problems as well), this is a work-around that uses cat
to transfer single files. It uses only the ssh
package.
The idea is to use cat without arguments to read from standard input. In our session object we provide our local file as standard input. Then we pipe cat's output with >
to the desired file.
The reverse way is similar, this time we intercept standard output of our session object. We cat the remote file and copy the session's standard output to our local file.
Here is the code:
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
}