scp

package module
v0.0.0-...-3a0c9dc Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 1, 2019 License: ISC Imports: 6 Imported by: 0

README

scp

import "github.com/Whiteblock/scp"

Package scp provides a simple interface to copying files over a go.crypto/ssh session.

func Copy

func Copy(size int64, mode os.FileMode, fileName string, contents io.Reader, destinationPath string, session *ssh.Session) error

func CopyPath

func CopyPath(filePath, destinationPath string, session *ssh.Session) error

Documentation

Overview

Package scp provides a simple interface to copying files over a go.crypto/ssh session.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Copy

func Copy(size int64, mode os.FileMode, fileName string, contents io.Reader, destinationPath string, session *ssh.Session) error

func CopyPath

func CopyPath(filePath, destinationPath string, session *ssh.Session) error
Example
package main

import (
	"fmt"
	"io/ioutil"
	"log"
	"net"
	"os"

	"golang.org/x/crypto/ssh"
	"golang.org/x/crypto/ssh/agent"

	"github.com/tmc/scp"
)

func getAgent() (agent.Agent, error) {
	agentConn, err := net.Dial("unix", os.Getenv("SSH_AUTH_SOCK"))
	return agent.NewClient(agentConn), err
}

func main() {
	f, _ := ioutil.TempFile("", "")
	fmt.Fprintln(f, "hello world")
	f.Close()
	defer os.Remove(f.Name())
	defer os.Remove(f.Name() + "-copy")

	agent, err := getAgent()
	if err != nil {
		log.Fatalln("Failed to connect to SSH_AUTH_SOCK:", err)
	}

	client, err := ssh.Dial("tcp", "127.0.0.1:22", &ssh.ClientConfig{
		User: os.Getenv("USER"),
		Auth: []ssh.AuthMethod{
			ssh.PublicKeysCallback(agent.Signers),
		},
		HostKeyCallback: ssh.InsecureIgnoreHostKey(), // FIXME: please be more secure in checking host keys
	})
	if err != nil {
		log.Fatalln("Failed to dial:", err)
	}

	session, err := client.NewSession()
	if err != nil {
		log.Fatalln("Failed to create session: " + err.Error())
	}

	dest := f.Name() + "-copy"
	err = scp.CopyPath(f.Name(), dest, session)
	if _, err := os.Stat(dest); os.IsNotExist(err) {
		fmt.Printf("no such file or directory: %s", dest)
	} else {
		fmt.Println("success")
	}
}
Output:

success

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL