ssh

package
v0.13.0 Latest Latest
Warning

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

Go to latest
Published: Mar 11, 2020 License: BSD-3-Clause Imports: 7 Imported by: 1

Documentation

Overview

Package ssh provide a wrapper for golang.org/x/crypto/ssh.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

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

Client for SSH connection.

func NewClient

func NewClient(cfg *ClientConfig) (cl *Client, err error)

NewClient create a new SSH connection using predefined configuration.

func (*Client) Execute

func (cl *Client) Execute(cmd string) (err error)

Execute a command on remote server.

func (*Client) Get

func (cl *Client) Get(remote, local string) (err error)

Get copy file from remote into local storage.

The local file should be use the absolute path, or relative to the file in ClientConfig.WorkingDir.

func (*Client) Put

func (cl *Client) Put(local, remote string) (err error)

Put copy a file from local storage to remote using scp command.

The local file should be use the absolute path, or relative to the file in ClientConfig.WorkingDir.

func (*Client) String

func (cl *Client) String() string

type ClientConfig

type ClientConfig struct {
	// Environments contains system environment variables that will be
	// passed to Execute().
	Environments map[string]string

	// WorkingDir contains the directory where the SSH client started.
	// This value is required when client want to copy file from/to
	// remote.
	// This field is optional, default to current working directory from
	// os.Getwd() or user's home directory.
	WorkingDir string

	// PrivateKeyFile contains path to private key file.
	// This field is optional, default to ".ssh/id_rsa" in user's home
	// directory.
	PrivateKeyFile string

	// RemoteUser contains the user name in remote system.
	// This field is mandatory.
	RemoteUser string

	// RemoteHost contains the IP address or host name of remote system.
	// This field is mandatory.
	RemoteHost string

	// RemotePort contains the port address of remote SSH server.
	// This field is optional, default to 22.
	RemotePort int
	// contains filtered or unexported fields
}

ClientConfig contains the configuration to create SSH connection and the environment where the SSH program started.

Example
cfg := &ClientConfig{
	WorkingDir:     "/tmp",
	PrivateKeyFile: "testdata/example.pem",
	RemoteUser:     "hodor",
	RemoteHost:     "127.0.0.1",
}

err := cfg.initialize()
if err != nil {
	log.Fatal(err)
}

fmt.Printf("WorkingDir: %s\n", cfg.WorkingDir)
fmt.Printf("PrivateKeyFile: %s\n", cfg.PrivateKeyFile)
fmt.Printf("RemoteUser: %s\n", cfg.RemoteUser)
fmt.Printf("RemoteHost: %s\n", cfg.RemoteHost)
fmt.Printf("RemotePort: %d\n", cfg.RemotePort)
fmt.Printf("remoteAddr: %s\n", cfg.remoteAddr)
Output:

WorkingDir: /tmp
PrivateKeyFile: testdata/example.pem
RemoteUser: hodor
RemoteHost: 127.0.0.1
RemotePort: 22
remoteAddr: 127.0.0.1:22

Jump to

Keyboard shortcuts

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