ssh

package
v0.4.36 Latest Latest
Warning

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

Go to latest
Published: Jun 17, 2026 License: MIT Imports: 22 Imported by: 0

Documentation

Index

Constants

View Source
const DefaultCommandTimeout = 30 * time.Minute

Variables

This section is empty.

Functions

func GetKnownHostsPath added in v0.0.4

func GetKnownHostsPath() (string, error)

GetKnownHostsPath returns the path to Tako's known_hosts file

func InteractivePrompt added in v0.0.4

func InteractivePrompt(host, fingerprint, keyType string) (bool, error)

InteractivePrompt prompts the user to accept an unknown host key Use this with SetPromptFunc for interactive CLI sessions

func RemoveHostKey added in v0.2.0

func RemoveHostKey(host string) error

RemoveHostKey removes a host key from Tako's known_hosts file This should be called when recreating a server to allow clean reconnection.

func SetGlobalHostKeyMode added in v0.0.4

func SetGlobalHostKeyMode(mode HostKeyMode)

SetGlobalHostKeyMode sets the global host key verification mode

Types

type Client

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

Client wraps an SSH connection with additional functionality

func NewClient

func NewClient(host string, port int, user string, keyPath string) (*Client, error)

NewClient creates a new SSH client with key-based authentication Also tries ssh-agent for passphrase-protected keys

func NewClientFromConfig added in v0.0.3

func NewClientFromConfig(cfg ServerConfig) (*Client, error)

NewClientFromConfig creates a new SSH client from server configuration Automatically chooses between key-based and password-based authentication

func NewClientWithAuth added in v0.0.3

func NewClientWithAuth(host string, port int, user string, keyPath string, password string) (*Client, error)

NewClientWithAuth creates a new SSH client with either key or password authentication If both keyPath and password are provided, key-based auth is preferred Also tries ssh-agent as fallback for passphrase-protected keys

func NewClientWithPassword added in v0.0.3

func NewClientWithPassword(host string, port int, user string, password string) (*Client, error)

NewClientWithPassword creates a new SSH client with password-based authentication

func (*Client) Close

func (c *Client) Close() error

Close closes the SSH connection and any associated resources

func (*Client) Connect

func (c *Client) Connect() error

Connect establishes the SSH connection with retry logic and optimized TCP settings

func (*Client) CopyDirectory

func (c *Client) CopyDirectory(localPath, remotePath string) error

CopyDirectory copies a directory from local to remote server

func (*Client) CopyFile

func (c *Client) CopyFile(localPath, remotePath string) error

CopyFile copies a file from local to remote server

func (*Client) CopyFileWithMode

func (c *Client) CopyFileWithMode(localPath, remotePath string, mode os.FileMode) error

CopyFileWithMode uploads a file to the remote server with specific permissions

func (*Client) CopyFromRemote

func (c *Client) CopyFromRemote(remotePath, localPath string) error

CopyFromRemote copies a file from remote to local

func (*Client) Execute

func (c *Client) Execute(cmd string) (string, error)

Execute runs a command on the remote server

func (*Client) ExecuteInteractive added in v0.2.5

func (c *Client) ExecuteInteractive(cmd string) error

ExecuteInteractive runs a command interactively with a remote PTY. It puts the local terminal in raw mode and forwards stdin/stdout/stderr bidirectionally, supporting full interactive sessions (shells, editors, etc.).

func (*Client) ExecuteStream

func (c *Client) ExecuteStream(cmd string, stdout, stderr io.Writer) error

ExecuteStream runs a command and streams output in real-time

func (*Client) ExecuteWithContext

func (c *Client) ExecuteWithContext(ctx context.Context, cmd string) (string, error)

ExecuteWithContext runs a command with context support for cancellation

func (*Client) ExecuteWithInput added in v0.3.0

func (c *Client) ExecuteWithInput(ctx context.Context, cmd string, input io.Reader) (string, error)

ExecuteWithInput runs a command on the remote server, streams input to stdin, and returns combined stdout/stderr output.

func (*Client) Host added in v0.1.4

func (c *Client) Host() string

Host returns the host address of the SSH connection

func (*Client) IsConnected

func (c *Client) IsConnected() bool

IsConnected checks if the client is connected

func (*Client) IsHealthy added in v0.1.4

func (c *Client) IsHealthy() bool

IsHealthy checks if the connection is still alive by sending a keepalive request This helps detect stale connections that appear connected but are actually dead

func (*Client) Port added in v0.1.4

func (c *Client) Port() int

Port returns the port of the SSH connection

func (*Client) StartStream

func (c *Client) StartStream(cmd string) (*StreamingSession, error)

StartStream creates a new streaming session for long-running commands

func (*Client) UploadReader

func (c *Client) UploadReader(reader io.Reader, remotePath string, mode os.FileMode) error

UploadReader uploads content from a reader to a remote file

type HostKeyMode added in v0.0.4

type HostKeyMode int

HostKeyMode controls host key verification behavior

const (
	// HostKeyModeTOFU trusts on first use, verifies on subsequent connections (default)
	HostKeyModeTOFU HostKeyMode = iota
	// HostKeyModeStrict requires host to already be in known_hosts
	HostKeyModeStrict
	// HostKeyModeAsk prompts user for unknown hosts (interactive only)
	HostKeyModeAsk
)

func GetGlobalHostKeyMode added in v0.0.4

func GetGlobalHostKeyMode() HostKeyMode

GetGlobalHostKeyMode returns the current global host key verification mode

func ParseHostKeyMode added in v0.0.4

func ParseHostKeyMode(s string) (HostKeyMode, error)

ParseHostKeyMode parses a string into HostKeyMode

func (HostKeyMode) String added in v0.0.4

func (m HostKeyMode) String() string

String returns string representation of HostKeyMode

type HostKeyVerifier added in v0.0.4

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

HostKeyVerifier provides host key verification with TOFU support

func GetDefaultVerifier added in v0.0.4

func GetDefaultVerifier() (*HostKeyVerifier, error)

GetDefaultVerifier returns the default host key verifier

func NewHostKeyVerifier added in v0.0.4

func NewHostKeyVerifier(mode HostKeyMode) (*HostKeyVerifier, error)

NewHostKeyVerifier creates a new host key verifier

func (*HostKeyVerifier) GetCallback added in v0.0.4

func (v *HostKeyVerifier) GetCallback() ssh.HostKeyCallback

GetCallback returns an ssh.HostKeyCallback for use with ssh.ClientConfig

func (*HostKeyVerifier) SetMode added in v0.0.4

func (v *HostKeyVerifier) SetMode(mode HostKeyMode)

SetMode sets the verification mode

func (*HostKeyVerifier) SetPromptFunc added in v0.0.4

func (v *HostKeyVerifier) SetPromptFunc(fn func(host, fingerprint, keyType string) (bool, error))

SetPromptFunc sets the function to prompt user for unknown hosts

type MultiplexConfig

type MultiplexConfig struct {
	ControlPath    string
	ControlPersist time.Duration
	MaxSessions    int
}

MultiplexConfig holds multiplexing configuration

type Multiplexer

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

Multiplexer manages SSH connection multiplexing for improved performance

func NewMultiplexer

func NewMultiplexer() *Multiplexer

NewMultiplexer creates a new SSH multiplexer

func (*Multiplexer) Cleanup

func (m *Multiplexer) Cleanup()

Cleanup removes stale connections

func (*Multiplexer) CloseAll

func (m *Multiplexer) CloseAll()

CloseAll closes all multiplexed connections

func (*Multiplexer) GetConnection

func (m *Multiplexer) GetConnection(host string, port int, user string, sshKey string) (*MuxConnection, error)

GetConnection retrieves or creates a multiplexed connection

type MuxConnection

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

MuxConnection represents a multiplexed SSH connection

func (*MuxConnection) Close

func (c *MuxConnection) Close() error

Close closes the multiplexed connection

func (*MuxConnection) Execute

func (c *MuxConnection) Execute(command string) (string, error)

Execute runs a command using the multiplexed connection

func (*MuxConnection) IsHealthy

func (c *MuxConnection) IsHealthy() bool

IsHealthy checks if the multiplexed connection is still active

type Pool

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

Pool manages a pool of SSH connections

func NewPool

func NewPool() *Pool

NewPool creates a new SSH connection pool

func (*Pool) CloseAll

func (p *Pool) CloseAll() error

CloseAll closes all connections in the pool

func (*Pool) Get

func (p *Pool) Get(host string, port int, user string) *Client

Get retrieves a client from the pool

func (*Pool) GetOrCreate

func (p *Pool) GetOrCreate(host string, port int, user string, keyPath string) (*Client, error)

GetOrCreate gets an existing client from the pool or creates a new one Uses proper locking to avoid race conditions with double-checked locking

func (*Pool) GetOrCreateWithAuth added in v0.0.3

func (p *Pool) GetOrCreateWithAuth(host string, port int, user string, keyPath string, password string) (*Client, error)

GetOrCreateWithAuth gets an existing client from the pool or creates a new one Supports both key-based and password-based authentication Automatically removes and replaces unhealthy connections

func (*Pool) Remove

func (p *Pool) Remove(host string, port int, user string) error

Remove removes a client from the pool and closes it

type ServerConfig added in v0.0.3

type ServerConfig struct {
	Host     string
	Port     int
	User     string
	SSHKey   string
	Password string
}

ServerConfig represents SSH connection parameters This mirrors config.ServerConfig to avoid circular imports

type StreamingSession

type StreamingSession struct {
	Stdout io.Reader
	Stderr io.Reader
	// contains filtered or unexported fields
}

StreamingSession represents a streaming SSH session

func (*StreamingSession) Close

func (s *StreamingSession) Close() error

Close closes the streaming session

func (*StreamingSession) Wait added in v0.3.0

func (s *StreamingSession) Wait() error

Wait waits for the streaming command to finish.

func (*StreamingSession) WaitContext added in v0.4.27

func (s *StreamingSession) WaitContext(ctx context.Context) error

WaitContext waits for the streaming command to finish or for ctx to expire.

Jump to

Keyboard shortcuts

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