ssh

package
v0.0.0-...-d376067 Latest Latest
Warning

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

Go to latest
Published: Oct 27, 2021 License: GPL-3.0 Imports: 14 Imported by: 0

Documentation

Overview

SSH

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func PasswordKey

func PasswordKey(username string, password string) (ssh.ClientConfig, error)

func PrivateKey

func PrivateKey(username string, path string) (ssh.ClientConfig, error)

PrivateKey Loads a private and public key from "path" and returns a SSH ClientConfig to authenticate with the server

func PrivateKeyWithPassphrase

func PrivateKeyWithPassphrase(username string, passpharase []byte, path string) (ssh.ClientConfig, error)

Types

type Client

type Client struct {
	// the host to connect to
	Host string

	// the client config to use
	ClientConfig *ssh.ClientConfig

	// stores the SSH session while the connection is running
	Session *ssh.Session

	// stores the SSH connection itself in order to close it after transfer
	Conn ssh.Conn

	// the clients waits for the given timeout until given up the connection
	Timeout time.Duration

	// the absolute path to the remote SCP binary
	RemoteBinary string

	// password for sudo command
	Password string
}

func NewClient

func NewClient(host string, config *ssh.ClientConfig) Client

It has a default timeout of one minute.

func NewClientWithBasicAuth

func NewClientWithBasicAuth(host, username, password string) Client

use username and password.

func NewClientWithBasicAuthAndTimeout

func NewClientWithBasicAuthAndTimeout(host, username, password string, timeout time.Duration) Client

user username and password with timeout

func NewClientWithTimeout

func NewClientWithTimeout(host string, config *ssh.ClientConfig, timeout time.Duration) Client

Returns a new ssh.Client with provides host, ssh.ClientConfig and timeout

func (*Client) Close

func (a *Client) Close()

func (*Client) Connect

func (a *Client) Connect() error

Connects to the remote SSH server, returns error if it couldn't establish a session to the SSH server

func (*Client) Copy

func (a *Client) Copy(r io.Reader, remotePath string, permissions string, size int64) error

Copies the contents of an io.Reader to a remote location

func (*Client) CopyFile

func (a *Client) CopyFile(fileReader io.Reader, remotePath string, permissions string) error

Copies the contents of an io.Reader to a remote location, the length is determined by reading the io.Reader until EOF if the file length in know in advance please use "Copy" instead

func (*Client) CopyFromFile

func (a *Client) CopyFromFile(file os.File, remotePath string, permissions string) error

Copies the contents of an os.File to a remote location, it will get the length of the file by looking it up from the filesystem

func (*Client) Run

func (a *Client) Run(cmd string) (string, string, error)

Run run a shell command on remote server.

func (*Client) RunWithSudo

func (a *Client) RunWithSudo(cmd string) (string, string, error)

Run run a shell command on remote server with sudo.

type ClientConfigurer

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

A struct containing all the configuration options

func NewConfigurer

func NewConfigurer(host string, config *ssh.ClientConfig) *ClientConfigurer

Creates a new client configurer. It takes the required parameters: the host and the ssh.ClientConfig and returns a configurer populated with the default values for the optional parameters.

These optional parameters can be set by using the methods provided on the ClientConfigurer struct.

func NewConfigurerWithBasicAuth

func NewConfigurerWithBasicAuth(host, password string, config *ssh.ClientConfig) *ClientConfigurer

func (*ClientConfigurer) ClientConfig

func (c *ClientConfigurer) ClientConfig(config *ssh.ClientConfig) *ClientConfigurer

Alters the ssh.ClientConfig

func (*ClientConfigurer) Create

func (c *ClientConfigurer) Create() Client

Builds a client with the configuration stored within the ClientConfigurer

func (*ClientConfigurer) Host

func (c *ClientConfigurer) Host(host string) *ClientConfigurer

Alters the host of the client connects to

func (*ClientConfigurer) RemoteBinary

func (c *ClientConfigurer) RemoteBinary(path string) *ClientConfigurer

Sets the path of the location of the remote scp binary Defaults to: /usr/bin/scp

func (*ClientConfigurer) Timeout

func (c *ClientConfigurer) Timeout(timeout time.Duration) *ClientConfigurer

Changes the connection timeout. Defaults to one minute

type Response

type Response struct {
	Type    ResponseType
	Message string
}

There are tree types of responses that the remote can send back: ok, warning and error

The difference between warning and error is that the connection is not closed by the remote, however, a warning can indicate a file transfer failure (such as invalid destination directory) and such be handled as such.

All responses except for the `Ok` type always have a message (although these can be empty)

The remote sends a confirmation after every SCP command, because a failure can occur after every command, the response should be read and checked after sending them.

func ParseResponse

func ParseResponse(reader io.Reader) (Response, error)

Reads from the given reader (assuming it is the output of the remote) and parses it into a Response structure

func (*Response) GetMessage

func (r *Response) GetMessage() string

Returns the message the remote sent back

func (*Response) IsError

func (r *Response) IsError() bool

Returns true when the remote responded with an error

func (*Response) IsFailure

func (r *Response) IsFailure() bool

Returns true when the remote answered with a warning or an error

func (*Response) IsOk

func (r *Response) IsOk() bool

func (*Response) IsWarning

func (r *Response) IsWarning() bool

type ResponseType

type ResponseType = uint8
const (
	Ok      ResponseType = 0
	Warning ResponseType = 1
	Error   ResponseType = 2
)

Jump to

Keyboard shortcuts

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