ssh

package
v1.0.16 Latest Latest
Warning

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

Go to latest
Published: Jan 2, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package ssh provides SSH client functionality for connecting to Proxmox nodes and containers.

This package includes specialized support for different container types, including automatic detection and handling of NixOS containers which require special environment setup commands.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ExecuteLXCShell

func ExecuteLXCShell(user, nodeIP string, vmID int) error

ExecuteLXCShell opens an interactive session to an LXC container using 'pct enter'.

This is a convenience function that uses the default executor and context. For containers that require special handling (like NixOS), use ExecuteLXCShellWithVM.

Parameters:

  • user: SSH username for authentication to the Proxmox node
  • nodeIP: IP address or hostname of the Proxmox node hosting the container
  • vmID: Container ID number

Returns an error if the connection fails.

func ExecuteLXCShellWith

func ExecuteLXCShellWith(ctx context.Context, execer CommandExecutor, user, nodeIP string, vmID int, vm *api.VM) error

ExecuteLXCShellWith opens an interactive session to an LXC container with full control options.

This function provides comprehensive control over LXC container access with automatic OS detection and appropriate command selection:

For NixOS containers (OSType "nixos" or "nix"):

  • Uses 'pct exec' with environment initialization
  • Sources /etc/set-environment if present
  • Executes bash with proper environment setup

For other containers:

  • Uses standard 'pct enter' command
  • Provides direct container access

The function automatically sets TERM=xterm-256color for better terminal compatibility and displays appropriate completion messages based on the container type.

Example usage:

// Standard container
err := ExecuteLXCShellWith(ctx, executor, "root", "192.168.1.100", 101, nil)

// NixOS container with auto-detection
nixosVM := &api.VM{ID: 102, OSType: "nixos"}
err := ExecuteLXCShellWith(ctx, executor, "root", "192.168.1.100", 102, nixosVM)

Parameters:

  • ctx: Context for controlling execution lifetime and cancellation
  • execer: Command executor interface for running SSH commands
  • user: SSH username for authentication to the Proxmox node
  • nodeIP: IP address or hostname of the Proxmox node hosting the container
  • vmID: Container ID number
  • vm: Optional VM information for OS detection (nil for standard behavior)

Returns an error if the connection fails.

func ExecuteLXCShellWithVM

func ExecuteLXCShellWithVM(user, nodeIP string, vm *api.VM) error

ExecuteLXCShellWithVM opens an interactive session to an LXC container with automatic OS detection.

This function automatically detects NixOS containers and uses the appropriate command to properly initialize the container environment. For NixOS containers, it uses 'pct exec' with environment setup, while other containers use the standard 'pct enter'.

NixOS containers are identified by OSType values of "nixos" or "nix" in the VM configuration.

Parameters:

  • user: SSH username for authentication to the Proxmox node
  • nodeIP: IP address or hostname of the Proxmox node hosting the container
  • vm: VM/container information including OS type for detection

Returns an error if the connection fails.

func ExecuteNodeShell

func ExecuteNodeShell(user, nodeIP string) error

ExecuteNodeShell opens an interactive SSH session to a Proxmox node.

This is a convenience function that uses the default executor and context. For more control over execution, use ExecuteNodeShellWith.

Parameters:

  • user: SSH username for authentication
  • nodeIP: IP address or hostname of the target node

Returns an error if the SSH connection fails.

func ExecuteNodeShellWith

func ExecuteNodeShellWith(ctx context.Context, execer CommandExecutor, user, nodeIP string) error

ExecuteNodeShellWith opens an interactive SSH session to a Proxmox node with custom execution context.

This function provides full control over the execution context and command executor, making it suitable for testing and advanced use cases.

The function automatically sets TERM=xterm-256color for better terminal compatibility with modern terminal emulators and displays completion status after the session ends.

Parameters:

  • ctx: Context for controlling execution lifetime and cancellation
  • execer: Command executor interface for running SSH commands
  • user: SSH username for authentication
  • nodeIP: IP address or hostname of the target node

Returns an error if the SSH connection fails.

func ExecuteQemuShell

func ExecuteQemuShell(user, vmIP string) error

ExecuteQemuShell attempts to connect to a QEMU VM using SSH directly.

This function connects directly to the VM's IP address rather than going through the Proxmox node. The VM must have network connectivity and SSH service running.

This is a convenience function that uses the default executor and context.

Parameters:

  • user: SSH username for authentication to the VM
  • vmIP: IP address of the target VM

Returns an error if the VM IP is empty or if the SSH connection fails.

func ExecuteQemuShellWith

func ExecuteQemuShellWith(ctx context.Context, execer CommandExecutor, user, vmIP string) error

ExecuteQemuShellWith attempts to connect to a QEMU VM using SSH with custom execution context.

This function provides full control over the execution context and command executor for connecting directly to QEMU VMs via SSH.

The function automatically sets TERM=xterm-256color for better terminal compatibility and displays completion status after the session ends.

Parameters:

  • ctx: Context for controlling execution lifetime and cancellation
  • execer: Command executor interface for running SSH commands
  • user: SSH username for authentication to the VM
  • vmIP: IP address of the target VM

Returns an error if the VM IP is empty or if the SSH connection fails.

Types

type CommandExecutor

type CommandExecutor interface {
	CommandContext(ctx context.Context, name string, args ...string) *exec.Cmd
}

CommandExecutor abstracts exec.CommandContext to allow dependency injection.

func NewDefaultExecutor

func NewDefaultExecutor() CommandExecutor

NewDefaultExecutor returns a new CommandExecutor using os/exec.

type Option

type Option func(*SSHClient)

Option configures SSHClient behavior during initialization.

func WithExecutor

func WithExecutor(exec CommandExecutor) Option

WithExecutor sets a custom command executor for the SSH client. This is primarily used for testing to inject mock executors.

type SSHClient

type SSHClient struct {
	Host     string
	User     string
	Password string
	// contains filtered or unexported fields
}

SSHClient wraps SSH connection parameters and provides methods for establishing SSH connections to Proxmox nodes and containers.

The client supports various authentication methods through the underlying SSH command and can be configured with custom command executors for testing.

func NewSSHClient

func NewSSHClient(host, user, password string, opts ...Option) (*SSHClient, error)

NewSSHClient creates a new SSHClient instance with the specified connection parameters.

Authentication is handled by the underlying "ssh" command which may use SSH keys, SSH agent, or other configured authentication methods. The password parameter is currently not used but reserved for future functionality.

Example usage:

client, err := NewSSHClient("192.168.1.100", "root", "")
if err != nil {
	return err
}
err = client.Shell()

Parameters:

  • host: The target host IP address or hostname
  • user: The SSH username for authentication
  • password: Reserved for future use (currently unused)
  • opts: Optional configuration functions

Returns a configured SSHClient instance or an error if initialization fails.

func (*SSHClient) Shell

func (c *SSHClient) Shell() error

Shell opens an interactive SSH shell session on the configured host.

This method uses the local ssh command and inherits the current process's stdio streams, providing a fully interactive terminal experience.

Returns an error if the SSH connection fails or if the client is nil.

Jump to

Keyboard shortcuts

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