scplib

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jul 16, 2019 License: MIT Imports: 11 Imported by: 0

README

go-scplib

GoDoc TravisCI Go Report Card

scp library for golang

Usage

See GoDoc reference.

Projects using go-scplib

Example

package main

import (
    "fmt"
    "io/ioutil"
    "os"
    "time"

    "github.com/blacknon/go-scplib"
    "golang.org/x/crypto/ssh"
)

func main() {
    // Read Private key
    key, err := ioutil.ReadFile(os.Getenv("HOME") + "/.ssh/id_rsa")
    if err != nil {
        fmt.Fprintf(os.Stderr, "Unable to read private key: %v\n", err)
        os.Exit(1)
    }

    // Parse Private key
    signer, err := ssh.ParsePrivateKey(key)
    if err != nil {
        fmt.Fprintf(os.Stderr, "Unable to parse private key: %v\n", err)
        os.Exit(1)
    }

    // Create ssh client config
    config := &ssh.ClientConfig{
        User: "user",
        Auth: []ssh.AuthMethod{
            ssh.PublicKeys(signer),
        },
        HostKeyCallback: ssh.InsecureIgnoreHostKey(),
        Timeout:         60 * time.Second,
    }

    // Create ssh connection
    connection, err := ssh.Dial("tcp", "test-node:22", config)
    if err != nil {
        fmt.Fprintf(os.Stderr, "Failed to dial: %s\n", err)
        os.Exit(1)
    }
    defer connection.Close()

    // Create scp client
    scp := new(scplib.SCPClient)
    scp.Permission = false // copy permission with scp flag
    scp.Connection = connection

    // scp get file
    // scp.GetFile([]strint{"/From/Remote/Path1","/From/Remote/Path2"],"/To/Local/Path")
    err = scp.GetFile([]string{"/etc/passwd"}, "./passwd")
    if err != nil {
        fmt.Fprintf(os.Stderr, "Failed to scp get: %s\n", err)
        os.Exit(1)
    }

    // // Dir pattern (Snip)
    // err := scp.GetFile("/path/from/remote/dir", "./path/to/local/dir")
    // if err != nil {
    //  fmt.Fprintf(os.Stderr, "Failed to create session: %s\n", err)
    //  os.Exit(1)
    // }

    // scp put file
    // scp.PutFile("/From/Local/Path","/To/Remote/Path")
    err = scp.PutFile([]string{"./passwd"}, "./passwd_scp")
    if err != nil {
        fmt.Fprintf(os.Stderr, "Failed to scp put: %s\n", err)
        os.Exit(1)
    }

    // scp get file (to scp format data)
    // scp.GetData("/path/remote/path")
    getData, err := scp.GetData([]string{"/etc/passwd"})
    if err != nil {
        fmt.Fprintf(os.Stderr, "Failed to scp put: %s\n", err)
        os.Exit(1)
    }

    fmt.Println(getData)

    // scp put file (to scp format data)
    // scp.GetData(Data,"/path/remote/path")
    err = scp.PutData(getData, "./passwd_data")
    if err != nil {
        fmt.Fprintf(os.Stderr, "Failed to scp put: %s\n", err)
        os.Exit(1)
    }
}

Documentation

Overview

Package scplib is a library for exchanging data with scp in golang.

Example
// Read Private key
key, err := ioutil.ReadFile(os.Getenv("HOME") + "/.ssh/id_rsa")
if err != nil {
	fmt.Fprintf(os.Stderr, "Unable to read private key: %v\n", err)
	os.Exit(1)
}

// Parse Private key
signer, err := ssh.ParsePrivateKey(key)
if err != nil {
	fmt.Fprintf(os.Stderr, "Unable to parse private key: %v\n", err)
	os.Exit(1)
}

// Create ssh client config
config := &ssh.ClientConfig{
	User: "user",
	Auth: []ssh.AuthMethod{
		ssh.PublicKeys(signer),
	},
	HostKeyCallback: ssh.InsecureIgnoreHostKey(),
	Timeout:         60 * time.Second,
}

// Create ssh connection
connection, err := ssh.Dial("tcp", "test-node:22", config)
if err != nil {
	fmt.Fprintf(os.Stderr, "Failed to dial: %s\n", err)
	os.Exit(1)
}
defer connection.Close()

// Create scp client
scp := new(scplib.SCPClient)
scp.Permission = false // copy permission with scp flag
scp.Connection = connection

// scp get file
// scp.GetFile("/From/Remote/Path","/To/Local/Path")
err = scp.GetFile([]string{"/etc/passwd"}, "./passwd")
if err != nil {
	fmt.Fprintf(os.Stderr, "Failed to scp get: %s\n", err)
	os.Exit(1)
}

// // Dir pattern snip // this sample comment out
// err := scp.GetFile("/path/from/remote/dir", "./path/to/local/dir")
// if err != nil {
// 	fmt.Fprintf(os.Stderr, "Failed to create session: %s\n", err)
// 	os.Exit(1)
// }

// scp put file
// scp.PutFile("/From/Local/Path","/To/Remote/Path")
err = scp.PutFile([]string{"./passwd"}, "./passwd_scp")
if err != nil {
	fmt.Fprintf(os.Stderr, "Failed to scp put: %s\n", err)
	os.Exit(1)
}

// scp get file (to scp format data)
// scp.GetData("/path/remote/path")
getData, err := scp.GetData([]string{"/etc/passwd"})
if err != nil {
	fmt.Fprintf(os.Stderr, "Failed to scp put: %s\n", err)
	os.Exit(1)
}

fmt.Println(getData)

// scp put file (to scp format data)
// scp.GetData(Data,"/path/remote/path")
err = scp.PutData(getData, "./passwd_data")
if err != nil {
	fmt.Fprintf(os.Stderr, "Failed to scp put: %s\n", err)
	os.Exit(1)
}
Output:

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type SCPClient

type SCPClient struct {
	Connection *ssh.Client
	Session    *ssh.Session
	Permission bool
}

SCPClient save credentials and use scp from method.

func (*SCPClient) GetData

func (s *SCPClient) GetData(fromPaths []string) (data *bytes.Buffer, err error)

GetData get and return scp format data(remote to local).

example:

scp.GetData("/path/remote/path")
Example
var connection *ssh.Client

// Create scp client
scp := new(scplib.SCPClient)
scp.Permission = false      // copy permission with scp flag
scp.Connection = connection // *ssh.Client

// Get /etc/passwd from remote machine
getData, err := scp.GetData([]string{"/etc/passwd"})
if err != nil {
	fmt.Fprintf(os.Stderr, "Failed to scp put: %s\n", err)
	os.Exit(1)
}

// println getData
fmt.Println(getData)
Output:

C0644 1561 passwd
root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
bin:x:2:2:bin:/bin:/usr/sbin/nologin
sys:x:3:3:sys:/dev:/usr/sbin/nologin
...

func (*SCPClient) GetFile

func (s *SCPClient) GetFile(fromPaths []string, toPath string) (err error)

GetFile get file data to file (remote to Local).

example:

scp.GetFile("/From/Remote/Path","/To/Local/Path")
Example
var connection *ssh.Client

// Create scp client
scp := new(scplib.SCPClient)
scp.Permission = false      // copy permission with scp flag
scp.Connection = connection // *ssh.Client

// Get /etc/passwd from remote machine, and copy to ./passwd
err := scp.GetFile([]string{"/etc/passwd"}, "./passwd")
if err != nil {
	fmt.Fprintf(os.Stderr, "Failed to scp get: %s\n", err)
	os.Exit(1)
}
Output:

func (*SCPClient) PutData

func (s *SCPClient) PutData(fromData *bytes.Buffer, toPath string) (err error)

PutData put data of scp format as a file(local to remote).

example:

scp.PutData(buffer(scp format data),"/path/remote/path")
Example
var connection *ssh.Client

// Create scp client
scp := new(scplib.SCPClient)
scp.Permission = false      // copy permission with scp flag
scp.Connection = connection // *ssh.Client

// Get /etc/passwd from remote machine
getData, err := scp.GetData([]string{"/etc/passwd"})
if err != nil {
	fmt.Fprintf(os.Stderr, "Failed to scp put: %s\n", err)
	os.Exit(1)
}

// getData Value...
// C0644 1561 passwd
// root:x:0:0:root:/root:/bin/bash
// daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
// bin:x:2:2:bin:/bin:/usr/sbin/nologin
// sys:x:3:3:sys:/dev:/usr/sbin/nologin
// ...

// Put getData
err = scp.PutData(getData, "./passwd_data")
if err != nil {
	fmt.Fprintf(os.Stderr, "Failed to scp put: %s\n", err)
	os.Exit(1)
}
Output:

func (*SCPClient) PutFile

func (s *SCPClient) PutFile(fromPaths []string, toPath string) (err error)

PutFile is put file to remote path.

example:

scp.PutFile("/From/Local/Path","/To/Remote/Path")
Example
var connection *ssh.Client

// Create scp client
scp := new(scplib.SCPClient)
scp.Permission = false      // copy permission with scp flag
scp.Connection = connection // *ssh.Client

// Put ./passwd to remote machine `./passwd_scp`
err := scp.PutFile([]string{"./passwd"}, "./passwd_scp")
if err != nil {
	fmt.Fprintf(os.Stderr, "Failed to scp put: %s\n", err)
	os.Exit(1)
}
Output:

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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