Documentation
¶
Overview ¶
Package ssh provides SSH connectivity utilities for remote server automation. It wraps github.com/sfreiberg/simplessh 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 ¶
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
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.
If cfg.Chdir is set, the command is wrapped with cd <dir> && <command>. If cfg.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>
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
SetRunFunc sets a custom function for executing 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 ¶
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>.