scp

package module
v1.1.5 Latest Latest
Warning

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

Go to latest
Published: Oct 8, 2023 License: Apache-2.0 Imports: 12 Imported by: 2

README

go-scp

使用go-scp非常容易实现在两个host之间copy文件/文件夹. go-scp基于golang.org/x/crypto/ssh包和remote host建立一个安全的连接,通过SCP协议复制文件.

Example

package main

import (
	"golang.org/x/crypto/ssh"
	"net"
)

var (
	addr     = "192.168.0.102:22"
	user     = "root"
	password = "password"
)

func main() {

	cfg := &ssh.ClientConfig{
		Config: ssh.Config{},
		User:   user,
		Auth: []ssh.AuthMethod{
			ssh.Password(password),
		},
		HostKeyCallback: func(hostname string, remote net.Addr, key ssh.PublicKey) error {
			return nil
		},
	}

	scp, err := New(addr, cfg)
	checkErr(err)
	defer scp.Close()

	err = scp.Upload("testdata", "/root/scp")
	checkErr(err)
	err = scp.Download("/root/scp", "testdata")
	checkErr(err)
	return
}

func checkErr(err error) {
	if err != nil {
		panic(err)
	}
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type FileHandler added in v1.1.3

type FileHandler func(path string, mode os.FileMode, reader io.Reader) error

type Msg

type Msg string

func (Msg) FileInfo

func (m Msg) FileInfo() (mode os.FileMode, size int64, filename string, err error)

func (Msg) String

func (m Msg) String() string

type Option added in v1.1.0

type Option func(s *SCP)

func WithRemoteSShBinaryPath added in v1.1.0

func WithRemoteSShBinaryPath(path string) Option

func WithSFTP added in v1.1.0

func WithSFTP(enable bool) Option

type Resp

type Resp struct {
	Type RespType
	Msg  Msg
}

func NewDirBegin

func NewDirBegin(mode os.FileMode, dirname string) *Resp

func NewDirEnd

func NewDirEnd() *Resp

func NewErrorRsp

func NewErrorRsp(msg string) *Resp

func NewFile

func NewFile(mode os.FileMode, filename string, size int64) *Resp

func NewOkRsp

func NewOkRsp() *Resp

func NewWarnRsp

func NewWarnRsp(msg string) *Resp

func ReadResp

func ReadResp(reader io.Reader) (*Resp, error)

func (*Resp) GetMessage

func (rsp *Resp) GetMessage() Msg

func (*Resp) IsDir

func (rsp *Resp) IsDir() bool

func (*Resp) IsEndDir

func (rsp *Resp) IsEndDir() bool

func (*Resp) IsError

func (rsp *Resp) IsError() bool

func (*Resp) IsFailure

func (rsp *Resp) IsFailure() bool

func (*Resp) IsFile

func (rsp *Resp) IsFile() bool

func (*Resp) IsOk

func (rsp *Resp) IsOk() bool

func (*Resp) IsWarning

func (rsp *Resp) IsWarning() bool

func (*Resp) Write

func (rsp *Resp) Write(w io.Writer) error

func (*Resp) WriteStream

func (rsp *Resp) WriteStream(w io.Writer, stream io.Reader) error

type RespType

type RespType = uint8
const (
	Ok      RespType = 0
	Warning RespType = 1
	Error   RespType = 2
	StreamC RespType = 'C'
	StreamD RespType = 'D'
	StreamE RespType = 'E'
)

type SCP

type SCP struct {
	// contains filtered or unexported fields
}

func New

func New(addr string, cfg *ssh.ClientConfig, ops ...Option) (*SCP, error)

func (*SCP) Close

func (s *SCP) Close() error

func (*SCP) Download

func (s *SCP) Download(remote string, local string) error

func (*SCP) DownloadWithHandler added in v1.1.3

func (s *SCP) DownloadWithHandler(remote string, local string, handler FileHandler) error

func (*SCP) Upload

func (s *SCP) Upload(local string, remote string) error

type Session added in v1.1.0

type Session interface {
	Close()
	Send(local string, remote string) error
	Recv(remote string, local string, handler FileHandler) error
}

func NewSFTPSession added in v1.1.0

func NewSFTPSession(sshCli *ssh.Client) (Session, error)

func NewScpSession added in v1.1.0

func NewScpSession(sshCli *ssh.Client, remoteScpBinary string) (Session, error)

Jump to

Keyboard shortcuts

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