ssh

package
v0.0.0-...-9cf5c93 Latest Latest
Warning

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

Go to latest
Published: Dec 18, 2018 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Conn

type Conn struct {
	*Session
	// contains filtered or unexported fields
}

Conn indicates an reusable SSH connection.

func NewConn

func NewConn(network, hostport string, cfg *ssh.ClientConfig) (*Conn, error)

NewConn initiates a new connection to hostport on network with given cfg. It returns the new Conn and any encountered error.

func (*Conn) Close

func (in *Conn) Close() error

Close shutdown the current ssh client and returns any error that encountered.

func (*Conn) CombinedOutput

func (in *Conn) CombinedOutput(ctx context.Context, cmd string, opt ...SessionOption) ([]byte, error)

CombinedOutput executes a command at foreground, returns a combined output and any encountered error.

func (*Conn) Output

func (in *Conn) Output(ctx context.Context, cmd string, opt ...SessionOption) ([]byte, error)

Output executes a command at foreground, returns stdout and any encountered error.

func (*Conn) Ping

func (in *Conn) Ping(ctx context.Context, opt ...SessionOption) (err error)

Ping launches a testing session with given opt if specified, and returns any encountered error.

func (*Conn) Reset

func (in *Conn) Reset() (err error)

Reset cleanups current session to prepare for next operation. It returns error if encountered any.

func (*Conn) Run

func (in *Conn) Run(ctx context.Context, cmd string, opt ...SessionOption) error

Run executes a command and returns its result as an error.

func (*Conn) Start

func (in *Conn) Start(ctx context.Context, cmd string, opt ...SessionOption) (err error)

Start runs a command at background, returns any encountered error.

func (*Conn) Wait

func (in *Conn) Wait() error

Wait standby until the running command exits. Keep in mind this should not be called if you are using Run, Output, or CombinedOutput.

type PtyOption

type PtyOption struct {
	// Term indicates the variant of the terminal. Such as: vt100, xterm,
	// xterm-256color, etc.
	Term string
	// Height indicates the window height.
	Height int
	// Width indicates the window width.
	Width int
}

PtyOption is the configuration of current PTY that is used by session.

type Session

type Session struct {

	// CommandFn is used for constructing command string. If it is not
	// specified, then will use the raw string directly.
	CommandFn func(string) string
	// PostStartFn will be performed after the session is started. Generally,
	// this is a function that enters password if it is required by some
	// command like `su`. It is for general purpose.
	PostStartFn func(*Session) error
	// Stdin is a piped standard input.
	Stdin io.WriteCloser
	// Stdout is a piped standard output.
	Stdout io.Reader
	// Stderr is a piped standard error.
	Stderr io.Reader
	// contains filtered or unexported fields
}

Session is a ssh session wrapper which enables you to do some advanced operations.

func NewSession

func NewSession(sess *ssh.Session) (*Session, error)

NewSession generates Session with sess, and returns any encountered error.

func (*Session) Close

func (in *Session) Close() error

Close terminates the current immediately.

func (*Session) CombinedOutput

func (in *Session) CombinedOutput(ctx context.Context, cmd string) ([]byte, error)

CombinedOutput performs given cmd with ctx, and waits until the session finishes. It returns the combined output and encountered error which could be an execution error or the ctx exceeds its deadline or has been cancelled.

func (*Session) Output

func (in *Session) Output(ctx context.Context, cmd string) ([]byte, error)

Output performs given cmd with ctx, and waits until the session finishes. It returns the standard output and encountered error which could be an execution error or the ctx exceeds its deadline or has been cancelled.

func (*Session) Run

func (in *Session) Run(ctx context.Context, cmd string) error

Run performs given cmd with ctx, and waits until the session finishes. It returns err if an execution error has encountered or the ctx exceeds its deadline or has been cancelled.

func (*Session) SetupPty

func (in *Session) SetupPty(opt PtyOption) error

SetupPty configures the current session pty with given opt, and returns any encountered error. If opt is empty, it will automatically be fulfilled with `vt100`, 60, 80. This is highly recommended to be called before performing operations.

func (*Session) Start

func (in *Session) Start(ctx context.Context, cmd string) (err error)

Start performs given cmd with ctx, returns err if it could not start the session.

func (*Session) Wait

func (in *Session) Wait() (err error)

Wait standby until the current session is finished. It returns err if an execution error has encountered or the ctx exceeds its deadline or has been cancelled.

type SessionOption

type SessionOption func(*Session) (*Session, error)

SessionOption is the extra option while initiating a new SSH session.

func WithSu

func WithSu(username, password string) SessionOption

WithSu generates a new SessionOption with given username and password.

func WithSudo

func WithSudo(password string) SessionOption

WithSudo generates a new SessionOption with given password. Omit password if there is no need to enter password for current user.

Jump to

Keyboard shortcuts

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