ssh

package
v0.21.0 Latest Latest
Warning

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

Go to latest
Published: May 4, 2026 License: AGPL-3.0 Imports: 11 Imported by: 0

Documentation

Overview

Package ssh provides SSH connectivity utilities for remote server automation. It uses golang.org/x/crypto/ssh with a simplified API for playbook-style operations where you connect, run commands, and disconnect.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func PrivateKeyPath

func PrivateKeyPath(sshKey string) string

PrivateKeyPath constructs the absolute path to an SSH private key file. It combines the current user's home directory with the .ssh directory and the provided key filename. The path uses forward slashes for SSH library compatibility.

func Run added in v0.9.0

func Run(cfg types.NodeConfig, cmd types.Command) (string, error)

Run connects to a node using NodeConfig and executes a command. It extracts SSH connection settings (SSHHost, SSHPort, SSHLogin, SSHKey) from the config and runs the command, returning the output.

Command-level settings (cmd.Chdir, cmd.BecomeUser, cmd.Required) take precedence over config-level settings (cfg.Chdir, cfg.BecomeUser).

If Chdir is set, the command is wrapped with cd <dir> && <command>. If BecomeUser is set, the command is wrapped with sudo -u <user>. The order is: cd first (outside sudo), then become (sudo), so the final command is:

cd <dir> && sudo -u <user> <command>

If Required is false and the command fails, the error is logged but not returned.

SAFETY: When cfg.IsDryRunMode is true, this function will NOT execute any commands on the server. Instead, it logs the command and returns "[dry-run]" as the output. This ensures no accidental changes in dry-run mode.

IMPORTANT: The callers must not rely on the safety net of this function. They should handle the dry-run mode themselves before calling this function. This is just a final safety net, as a last resort for implementation mistakes.

func SetRunFunc added in v0.10.0

func SetRunFunc(fn func(types.NodeConfig, types.Command) (string, error))

SetRunFunc sets a custom function for executing SSH commands. This is intended for testing purposes only. Call with nil to restore the default behavior.

func SetRunSingleCommandFunc added in v0.21.0

func SetRunSingleCommandFunc(fn func(host, port, user, key string, cmd types.Command, kexAlgorithms []string, hostKeyAlgorithms []string) (string, error))

SetRunSingleCommandFunc sets a custom function for executing single SSH commands. This is intended for testing purposes only. Call with nil to restore the default behavior.

Types

type Client

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

Client wraps an SSH connection with convenient methods for running commands.

func NewClient

func NewClient(host, port, user, key string) *Client

NewClient creates a new SSH client configuration. The host parameter should be just the hostname or IP (e.g., "db3.sinevia.com"). The port parameter is the SSH port (e.g., "22" or "40022"). The key parameter is just the filename (e.g., "2024_sinevia.prv"), which gets resolved to ~/.ssh/<key>.

func (*Client) Close

func (c *Client) Close() error

Close closes the SSH connection.

func (*Client) Connect

func (c *Client) Connect() error

Connect establishes the SSH connection. Must be called before Run or Close. Returns an error if the host is empty.

func (*Client) Run

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

Run executes a command on the remote server. Returns combined stdout/stderr output and any error.

func (*Client) WithHostKeyAlgorithms added in v0.19.0

func (c *Client) WithHostKeyAlgorithms(algorithms []string) *Client

func (*Client) WithKexAlgorithms added in v0.19.0

func (c *Client) WithKexAlgorithms(algorithms []string) *Client

Jump to

Keyboard shortcuts

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