Documentation
¶
Overview ¶
Production-ready Secure Copy Protocol (aka: SCP) implemented in Go with well documentation and neat dependency.
Index ¶
- Variables
- func NewSSHConfigFromPassword(username, password string) *ssh.ClientConfig
- func NewSSHConfigFromPrivateKey(username string, privPEM []byte, passphrase ...string) (cfg *ssh.ClientConfig, err error)
- type Client
- func (c *Client) CopyDirFromRemote(remoteDir, localDir string, opt *DirTransferOption) error
- func (c *Client) CopyDirToRemote(localDir string, remoteDir string, opt *DirTransferOption) error
- func (c *Client) CopyFileFromRemote(remoteFile, localFile string, opt *FileTransferOption) error
- func (c *Client) CopyFileToRemote(localFile string, remoteLoc string, opt *FileTransferOption) error
- func (c *Client) CopyFromRemote(remoteFile string, buffer io.Writer, opt *FileTransferOption) error
- func (c *Client) CopyToRemote(reader io.Reader, remoteTarget string, opt *FileTransferOption) error
- type ClientOption
- type DirTransferOption
- type FileTransferOption
- type KnownSize
Constants ¶
This section is empty.
Variables ¶
var ( // DefaultFilePerm holds default permission bits for transferred files. DefaultFilePerm = os.FileMode(0644) // DefaultDirPerm holds default permission bits for transferred directories. DefaultDirPerm = os.FileMode(0755) // ErrNoTransferOption indicate a non-nil TransferOption should be provided. ErrNoTransferOption = errors.New("scp: TransferOption is not provided") // DirectoryPreReads sets the num of pre-read files/directories for recursively transferring a directory. // Set it larger may speedup the transfer with lots of small files. // Do not set it too large or you will exceed the max open files limit. DirectoryPreReads = 10 )
var ( // DebugMode controls the debug output. // If true, the debug information of remote scp server // will be printed in Stderr. DebugMode = false )
var ( // ErrNoClientOption indicates a non-nil ClientOption should be provided ErrNoClientOption = errors.New("scp: ClientOption is not provided") )
Functions ¶
func NewSSHConfigFromPassword ¶
func NewSSHConfigFromPassword(username, password string) *ssh.ClientConfig
NewSSHConfigFromPassword returns a *ssh.ClientConfig with ssh.Password AuthMethod and 3 seconds timeout for connecting the server.
It *insecurely* ignores server's host key validation.
func NewSSHConfigFromPrivateKey ¶
func NewSSHConfigFromPrivateKey(username string, privPEM []byte, passphrase ...string) (cfg *ssh.ClientConfig, err error)
NewSSHConfigFromPrivateKey returns a *ssh.ClientConfig with ssh.PublicKey AuthMethod and 3 seconds timeout for connecting the server.
The passphrase is optional. If multiple passphrase are provided, only the first will be used.
If the private key is encrypted, it will return a ssh.PassphraseMissingError.
It *insecurely* ignores server's host key validation.
Types ¶
type Client ¶
type Client struct { // The underlying ssh client *ssh.Client // contains filtered or unexported fields }
Client has the "golang.org/x/crypto/ssh/Client" embedded, so it can be used as normal SSH client with additional SCP features.
func NewClient ¶
func NewClient(serverAddr string, sshCfg *ssh.ClientConfig, scpOpt *ClientOption) (*Client, error)
NewClient returns a SSH client with SCP capability.
The serverAddr should be in "host" or "host:port" format. If no port is supplied, the default port 22 will be used.
IPv6 serverAddr must be enclosed in square brackets, as in "[::1]" or "[::1]:22"
func NewClientFromExistingSSH ¶
func NewClientFromExistingSSH(existing *ssh.Client, scpOpt *ClientOption) (*Client, error)
NewClientFromExistingSSH returns a SSH client with SCP capability. It reuse the existing SSH connection without dialing
func (*Client) CopyDirFromRemote ¶
func (c *Client) CopyDirFromRemote(remoteDir, localDir string, opt *DirTransferOption) error
CopyDirFromRemote recursively copies a remote directory into local directory. The localDir must exist before copying.
For example:
- CopyDirFromRemote("/remote/dir1", "/local/dir2", &DirTransferOption{})
- Results: "remote/dir1/<contents>" -> "/local/dir2/<contents>"
func (*Client) CopyDirToRemote ¶
func (c *Client) CopyDirToRemote(localDir string, remoteDir string, opt *DirTransferOption) error
CopyDirToRemote recursively copies a directory to remoteDir.
func (*Client) CopyFileFromRemote ¶
func (c *Client) CopyFileFromRemote(remoteFile, localFile string, opt *FileTransferOption) error
CopyFileFromRemote copies a remoteFile as localFile.
If localFile does not exist, it will be automatically created. If localFile already exists, it will be truncated for writing. If localFile is a directory, the name of remoteFile will be used.
For example:
CopyFileFromRemote("/remote/file1", "/local/fileNotExist", &FileTransferOption)
Result: "/remote/file1" -> "/local/file2" The "fileNotExist" will be created.
CopyFileFromRemote("/remote/file1", "/local/fileExist", &FileTransferOption)
Result: "/remote/file1" -> "/local/fileExist" The "fileExist" will be truncated for writing.
CopyFileFromRemote("/remote/file1", "/local/dir", &FileTransferOption)
Result: "/remote/file1" -> "/local/dir/file1" The "file1" will be used as filename and stored under "/local/dir" directory. Note that "/local/dir" must exist in this case.
func (*Client) CopyFileToRemote ¶
func (c *Client) CopyFileToRemote(localFile string, remoteLoc string, opt *FileTransferOption) error
CopyFileToRemote copies a local file to remote location. It will automatically close the file after read.
func (*Client) CopyFromRemote ¶
CopyFromRemote copies a remote file into buffer.
Note that "PreserveProp" and "Perm" option does not take effect in this case.
func (*Client) CopyToRemote ¶
CopyToRemote copies content from reader to remoteTarget. The reader must implement "KnownSize" interface except *os.File.
Currently, it supports following readers:
- *os.File
- *strings.Reader
- *bytes.Reader
Note that the last part of remoteTarget will be used as filename if unspecified.
It's CALLER'S responsibility to CLOSE the file if an *os.File is supplied.
type ClientOption ¶
type ClientOption struct { // Use sudo to run remote scp server. // Default: false. Sudo bool // The scp remote server executable file. // Default: "scp". // // If your scp command is not in the default path, // specify it as "/path/to/scp". RemoteBinary string }
ClientOption contains several configurable options for SCP client.
type DirTransferOption ¶
type DirTransferOption struct { // Context for the directory transfer. // Can be both set with Timeout. // Default: no context Context context.Context // Timeout for transferring the whole directory. // Can be both set with Context. // Default: 0 (means no timeout) Timeout time.Duration // Preserve modification times and modes from the original file/directory. // Default: false PreserveProp bool // Limits the used bandwidth, specified in Kbit/s. // Default: 0 (Means no limit) // TODO: not implemented yet SpeedLimit int64 // ContentOnly skips creating the source directory on the receiving side, and // only transfers the source directory's contents. // Default: false ContentOnly bool }
DirTransferOption holds the transfer options for directory.
type FileTransferOption ¶
type FileTransferOption struct { // Context for the file transfer. // Can be both set with Timeout. // Default: no context Context context.Context // Timeout for transferring the file. // Can be both set with Context. // Default: 0 (Means no timeout) Timeout time.Duration // The permission bits for transferred file. // Override "PreserveProp" if specified. // Default: 0644 Perm os.FileMode // Preserve modification times and permission bits from the original file. // Only valid for file transfer. // Default: false PreserveProp bool // Limits the used bandwidth, specified in Kbit/s. // Default: 0 (Means no limit) // TODO: not implemented yet SpeedLimit int64 }
FileTransferOption holds the transfer options for file.