ssh

package
v1.137.0 Latest Latest
Warning

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

Go to latest
Published: Feb 26, 2026 License: Apache-2.0 Imports: 13 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func SingleKnownHost

func SingleKnownHost() ssh.HostKeyCallback

SingleKnownHost is a simple HostKeyCallback that stores the host key in memory on the first callback. Future callbacks verify that the host key hasn't changed.

Types

type Config

type Config struct {
	// ClientConfig is the standard SSH client config.
	ssh.ClientConfig

	// DialContext is used for opening TCP connections to the remote host. Defaults to a net.Dialer with 30s timeout.
	DialContext func(ctx context.Context, network, addr string) (net.Conn, error)
}

Config contains configuration for connecting to remote hosts.

func DefaultConfig

func DefaultConfig() *Config

DefaultConfig is the default SSH client Config used by Dial.

type Connection

type Connection struct {
	*ssh.Client

	SCP *scp.Client
	// contains filtered or unexported fields
}

Connection simplifies working with SSH connections for standard command execution and connection proxying. Use Dial to open a new Connection, and ensure to call Connection.Close() for cleanup.

func Dial

func Dial(ctx context.Context, addr string, opts ...Option) (*Connection, error)

Dial opens a new SSH Connection. Ensure to call Connection.Close() for cleanup.

func (*Connection) Copy added in v1.126.0

func (c *Connection) Copy(ctx context.Context, remotePath, permissions string, data []byte) error

Copy copies the given bytes to a file at remotePath with the given permissions.

func (*Connection) CopyFile added in v1.126.0

func (c *Connection) CopyFile(ctx context.Context, remotePath, permissions string, file fs.File) error

CopyFile copies the file to remotePath with the given permissions.

func (*Connection) Run

func (c *Connection) Run(ctx context.Context, command string) (io.Reader, io.Reader, error)

Run executes the given command on the remote host and returns stdout and stderr streams.

func (*Connection) RunAsUser added in v1.130.0

func (c *Connection) RunAsUser(user string) *Connection

RunAsUser configures the connection to run commands (Run, Copy, etc.) as the given user using sudo.

func (*Connection) RunWithStreams

func (c *Connection) RunWithStreams(ctx context.Context, stdin io.Reader, stdout, stderr io.Writer, command string) error

RunWithStreams executes the given command on the remote host with the configured streams.

func (*Connection) WithOutputPrefix added in v1.130.0

func (c *Connection) WithOutputPrefix(prefix string) *Connection

WithOutputPrefix configures the connection to add the given line prefix to stdout and stderr in Run and RunWithStreams.

type Option

type Option func(opts *Config) error

Option is an option that can be passed to Dial for customizing the client Config.

func WithPrivateKey

func WithPrivateKey(key *rsa.PrivateKey) Option

WithPrivateKey configures the client to authenticate using the given RSA private key.

func WithPrivateKeyBytes

func WithPrivateKeyBytes(key []byte) Option

WithPrivateKeyBytes configures the client to authenticate using the given PEM encoded private key.

func WithProxyConnection

func WithProxyConnection(conn *Connection) Option

WithProxyConnection configures the client to open the new TCP connection to the remote host via another open SSH connection.

func WithUser

func WithUser(user string) Option

WithUser configures the login user.

type PrefixedWriter added in v1.126.0

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

PrefixedWriter wraps an io.Writer, emitting the passed in prefix at the beginning of each new line. This can be useful when running multiple ssh.Connections concurrently - you can prefix the log output of each session by passing in a PrefixedWriter. This is used by Connection.WithOutputPrefix.

func NewPrefixedWriter added in v1.126.0

func NewPrefixedWriter(prefix string, writer io.Writer) *PrefixedWriter

NewPrefixedWriter creates a new PrefixedWriter.

func (*PrefixedWriter) Write added in v1.126.0

func (w *PrefixedWriter) Write(b []byte) (int, error)

Write implements io.Writer.

Jump to

Keyboard shortcuts

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