Skip to content

Instantly share code, notes, and snippets.

@rohanthewiz
Created February 22, 2020 18:06
Show Gist options
  • Save rohanthewiz/81d0b11a4e53fca7443e3d1573698e64 to your computer and use it in GitHub Desktop.
Save rohanthewiz/81d0b11a4e53fca7443e3d1573698e64 to your computer and use it in GitHub Desktop.
Copy files to a secure server
package main
import (
"fmt"
"github.com/bramvdbogaerde/go-scp"
"github.com/bramvdbogaerde/go-scp/auth"
"golang.org/x/crypto/ssh"
"os"
)
func main() {
clientCfg, _ := auth.PrivateKey("gouser", "/home/ro/.ssh/id_ecdsa", ssh.InsecureIgnoreHostKey())
client := scp.NewClient("gonotes.net:22", &clientCfg)
err := client.Connect()
if err != nil {
fmt.Println("Could not establish a connection to the remote server", err)
return
}
defer client.Close()
f, err := os.Open("/home/ro/xfr/test.txt")
if err != nil {
fmt.Println("Could not open test file")
return
}
err = client.CopyFile(f, "/home/gouser/xfr/test.txt", "0644")
if err != nil {
fmt.Println("Error while copying file")
return
}
fmt.Println("File copied successfully")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment