ssh

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Feb 8, 2026 License: MIT Imports: 27 Imported by: 0

Documentation

Overview

Package ssh provides SSH authentication and connection utilities.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetAgentKeyCount

func GetAgentKeyCount() (int, error)

GetAgentKeyCount returns the number of keys in the SSH agent

func IsAgentAvailable

func IsAgentAvailable() bool

IsAgentAvailable checks if SSH agent is available and has keys

func IsSSHAvailable

func IsSSHAvailable() bool

IsSSHAvailable checks if SSH command is available

func TestAgentConnection

func TestAgentConnection() error

TestAgentConnection tests the connection to the SSH agent

Types

type Agent added in v0.3.0

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

Agent represents an SSH agent connection

type AgentAuth

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

AgentAuth provides SSH agent-based authentication

func NewAgentAuth

func NewAgentAuth() (*AgentAuth, error)

NewAgentAuth creates a new agent-based authenticator

func (*AgentAuth) Close

func (aa *AgentAuth) Close() error

Close closes the agent connection

func (*AgentAuth) CreateSSHClientConfig

func (aa *AgentAuth) CreateSSHClientConfig(username string) *ssh.ClientConfig

CreateSSHClientConfig creates an SSH client config with agent authentication

func (*AgentAuth) GetAuthMethod

func (aa *AgentAuth) GetAuthMethod() ssh.AuthMethod

GetAuthMethod returns the SSH auth method for agent authentication

func (*AgentAuth) GetKeyFingerprints

func (aa *AgentAuth) GetKeyFingerprints() []string

GetKeyFingerprints returns fingerprints of all keys in the agent

func (*AgentAuth) GetKeyInfo

func (aa *AgentAuth) GetKeyInfo() []map[string]string

GetKeyInfo returns information about keys in the agent

func (*AgentAuth) GetSigners

func (aa *AgentAuth) GetSigners() []ssh.Signer

GetSigners returns SSH signers from the agent

func (*AgentAuth) HasKey

func (aa *AgentAuth) HasKey(fingerprint string) bool

HasKey checks if a key with the given fingerprint is available

func (*AgentAuth) RefreshSigners

func (aa *AgentAuth) RefreshSigners() error

RefreshSigners refreshes the list of signers from the agent

type Client added in v0.3.0

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

Client handles SSH connections to cluster nodes

func NewSSHClient

func NewSSHClient(config *Config) *Client

NewSSHClient creates a new SSH client

func (*Client) ConnectToNode added in v0.3.0

func (c *Client) ConnectToNode(ctx context.Context, hostname string) error

ConnectToNode opens an SSH connection to a cluster node

func (*Client) ConnectToNodeInTerminal added in v0.3.0

func (c *Client) ConnectToNodeInTerminal(hostname string) error

ConnectToNodeInTerminal opens SSH in the current terminal

func (*Client) ExecuteCommand added in v0.3.0

func (c *Client) ExecuteCommand(ctx context.Context, hostname, command string) (string, error)

ExecuteCommand executes a command on a remote node via SSH

func (*Client) GetNodeInfo added in v0.3.0

func (c *Client) GetNodeInfo(ctx context.Context, hostname string) (map[string]string, error)

GetNodeInfo retrieves basic information about a node

func (*Client) TestConnection added in v0.3.0

func (c *Client) TestConnection(ctx context.Context, hostname string) error

TestConnection tests SSH connectivity to a node

type Config added in v0.3.0

type Config struct {
	Username     string
	Port         int
	KeyFile      string
	ConfigFile   string
	Options      map[string]string
	Timeout      time.Duration
	UseAgent     bool              // Use SSH agent for authentication
	KeyManager   *KeyManager       // Key manager for advanced key handling
	ClientConfig *ssh.ClientConfig // Native SSH client config
}

Config holds SSH connection configuration

type Key added in v0.3.0

type Key struct {
	Name        string
	Path        string
	PublicPath  string
	Type        string
	Fingerprint string
	Comment     string
	IsLoaded    bool
	CreatedAt   time.Time
	LastUsed    time.Time
}

Key represents an SSH key pair

type KeyManager

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

KeyManager manages SSH keys and agent integration

func NewKeyManager

func NewKeyManager() (*KeyManager, error)

NewKeyManager creates a new SSH key manager

func (*KeyManager) Cleanup

func (km *KeyManager) Cleanup() error

Cleanup cleans up resources

func (*KeyManager) ConnectToAgent

func (km *KeyManager) ConnectToAgent() error

ConnectToAgent connects to the SSH agent

func (*KeyManager) DeleteKey

func (km *KeyManager) DeleteKey(name string) error

DeleteKey deletes an SSH key pair

func (*KeyManager) DisconnectFromAgent

func (km *KeyManager) DisconnectFromAgent() error

DisconnectFromAgent disconnects from the SSH agent

func (*KeyManager) DiscoverKeys

func (km *KeyManager) DiscoverKeys() error

DiscoverKeys discovers SSH keys in the SSH directory

func (*KeyManager) GenerateKey

func (km *KeyManager) GenerateKey(name, comment, keyType string, bits int) error

GenerateKey generates a new SSH key pair

func (*KeyManager) GetAgentInfo

func (km *KeyManager) GetAgentInfo() (map[string]interface{}, error)

GetAgentInfo returns information about the SSH agent

func (*KeyManager) GetAgentKeys

func (km *KeyManager) GetAgentKeys() ([]*agent.Key, error)

GetAgentKeys returns keys currently loaded in the SSH agent

func (*KeyManager) GetKey

func (km *KeyManager) GetKey(name string) (*SSHKey, error)

GetKey returns a specific SSH key

func (*KeyManager) GetKeyPath

func (km *KeyManager) GetKeyPath(name string) (string, error)

GetKeyPath returns the path to a specific key

func (*KeyManager) GetKeys

func (km *KeyManager) GetKeys() map[string]*SSHKey

GetKeys returns all discovered SSH keys

func (*KeyManager) GetSSHConfig

func (km *KeyManager) GetSSHConfig() (*SSHConfig, error)

GetSSHConfig generates SSH client configuration using available keys

func (*KeyManager) IsAgentConnected

func (km *KeyManager) IsAgentConnected() bool

IsAgentConnected returns true if connected to SSH agent

func (*KeyManager) IsKeyLoadedInAgent

func (km *KeyManager) IsKeyLoadedInAgent(fingerprint string) bool

IsKeyLoadedInAgent checks if a key is loaded in the agent

func (*KeyManager) LoadKeyToAgent

func (km *KeyManager) LoadKeyToAgent(name string) error

LoadKeyToAgent loads a specific key to the SSH agent

func (*KeyManager) LoadKeysToAgent

func (km *KeyManager) LoadKeysToAgent() error

LoadKeysToAgent loads all keys to the SSH agent

func (*KeyManager) RefreshKeyStatus

func (km *KeyManager) RefreshKeyStatus() error

RefreshKeyStatus refreshes the loaded status of all keys

func (*KeyManager) RemoveAllKeysFromAgent

func (km *KeyManager) RemoveAllKeysFromAgent() error

RemoveAllKeysFromAgent removes all keys from the SSH agent

func (*KeyManager) RemoveKeyFromAgent

func (km *KeyManager) RemoveKeyFromAgent(name string) error

RemoveKeyFromAgent removes a key from the SSH agent

func (*KeyManager) SetAutoLoad

func (km *KeyManager) SetAutoLoad(enable bool)

SetAutoLoad enables or disables automatic key loading

func (*KeyManager) StartAgent

func (km *KeyManager) StartAgent() error

StartAgent starts a new SSH agent if none is running

func (*KeyManager) StopAgent

func (km *KeyManager) StopAgent() error

StopAgent stops the SSH agent

type PersistentSession

type PersistentSession struct {
	ID           string            `json:"id"`
	Hostname     string            `json:"hostname"`
	Username     string            `json:"username"`
	LastActivity time.Time         `json:"last_activity"`
	ControlPath  string            `json:"control_path"`
	Tunnels      []SSHTunnel       `json:"tunnels"`
	Tags         map[string]string `json:"tags"` // User-defined tags
}

PersistentSession represents a session that can be saved/restored

type SSHAgent

type SSHAgent = Agent

type SSHClient

type SSHClient = Client

type SSHConfig

type SSHConfig = Config

func DefaultSSHConfig

func DefaultSSHConfig() *SSHConfig

DefaultSSHConfig returns a default SSH configuration for cluster environments

func (*SSHConfig) GetNativeClientConfig

func (c *SSHConfig) GetNativeClientConfig(_ string) (*ssh.ClientConfig, error)

GetNativeClientConfig returns a native SSH client config

func (*SSHConfig) WithAgent

func (c *SSHConfig) WithAgent(useAgent bool) *SSHConfig

WithAgent enables or disables SSH agent usage

func (*SSHConfig) WithKeyManager

func (c *SSHConfig) WithKeyManager(km *KeyManager) *SSHConfig

WithKeyManager sets a key manager for the SSH config

type SSHKey

type SSHKey = Key

type SSHSession

type SSHSession = Session

type SSHTerminal

type SSHTerminal struct {
	*tview.TextView
	// contains filtered or unexported fields
}

func NewSSHTerminal

func NewSSHTerminal(nodeID, hostname, username string, config *SSHConfig) (*SSHTerminal, error)

NewSSHTerminal creates a new SSH terminal

func (*SSHTerminal) Clear

func (t *SSHTerminal) Clear()

Clear clears the terminal buffer

func (*SSHTerminal) Close

func (t *SSHTerminal) Close() error

Close closes the terminal

func (*SSHTerminal) GetBuffer

func (t *SSHTerminal) GetBuffer() []string

GetBuffer returns the current terminal buffer

func (*SSHTerminal) GetInfo

func (t *SSHTerminal) GetInfo() TerminalInfo

GetInfo returns terminal information

func (*SSHTerminal) GetNodeID

func (t *SSHTerminal) GetNodeID() string

GetNodeID returns the node ID

func (*SSHTerminal) IsActive

func (t *SSHTerminal) IsActive() bool

IsActive returns whether the terminal is active

func (*SSHTerminal) Resize

func (t *SSHTerminal) Resize(_, _ int) error

Resize resizes the terminal

func (*SSHTerminal) SendCommand

func (t *SSHTerminal) SendCommand(command string) error

SendCommand sends a command to the terminal

func (*SSHTerminal) SetInputCapture

func (t *SSHTerminal) SetInputCapture(_ *tview.Application)

SetInputCapture sets up keyboard input handling

func (*SSHTerminal) SetOnClose

func (t *SSHTerminal) SetOnClose(callback func())

SetOnClose sets the callback for when the terminal closes

type SSHTunnel

type SSHTunnel = Tunnel

type Session added in v0.3.0

type Session struct {
	ID           string
	Hostname     string
	Username     string
	State        SessionState
	StartTime    time.Time
	LastActivity time.Time
	Command      *exec.Cmd
	Process      *os.Process
	ErrorMessage string
	ControlPath  string // For connection multiplexing
	Tunnels      []Tunnel
	// contains filtered or unexported fields
}

Session represents an active SSH session

type SessionManager

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

SessionManager manages SSH sessions with advanced features

func NewSessionManager

func NewSessionManager(config *SSHConfig) (*SessionManager, error)

NewSessionManager creates a new SSH session manager

func (*SessionManager) CloseAllSessions

func (sm *SessionManager) CloseAllSessions()

CloseAllSessions closes all active sessions

func (*SessionManager) CloseSession

func (sm *SessionManager) CloseSession(sessionID string) error

CloseSession closes an SSH session

func (*SessionManager) ConnectSession

func (sm *SessionManager) ConnectSession(sessionID string) error

ConnectSession establishes an SSH connection

func (*SessionManager) CreateSession

func (sm *SessionManager) CreateSession(hostname, username string) (*SSHSession, error)

CreateSession creates a new SSH session

func (*SessionManager) CreateTunnel

func (sm *SessionManager) CreateTunnel(sessionID string, localPort int, remoteHost string, remotePort int, tunnelType string) error

CreateTunnel creates an SSH tunnel for port forwarding

func (*SessionManager) EnablePersistence

func (sm *SessionManager) EnablePersistence(dataDir string) error

EnablePersistence enables session persistence with a custom data directory

func (*SessionManager) ExecuteCommand

func (sm *SessionManager) ExecuteCommand(sessionID, command string) (string, error)

ExecuteCommand executes a command in an SSH session

func (*SessionManager) GetNodeSystemInfo

func (sm *SessionManager) GetNodeSystemInfo(sessionID string) (map[string]string, error)

GetNodeSystemInfo retrieves comprehensive system information from a node

func (*SessionManager) GetSessionInfo

func (sm *SessionManager) GetSessionInfo(sessionID string) (*SSHSession, error)

GetSessionInfo returns detailed information about a session

func (*SessionManager) GetSessionTags

func (sm *SessionManager) GetSessionTags(sessionID string) (map[string]string, error)

GetSessionTags gets user-defined tags for a session

func (*SessionManager) GetSessions

func (sm *SessionManager) GetSessions() map[string]*SSHSession

GetSessions returns all active sessions

func (*SessionManager) MonitorSession

func (sm *SessionManager) MonitorSession(sessionID string, interval time.Duration) (<-chan SessionState, error)

MonitorSession continuously monitors a session and reports status

func (*SessionManager) SaveSessionTags

func (sm *SessionManager) SaveSessionTags(sessionID string, tags map[string]string) error

SaveSessionTags saves user-defined tags for a session

func (*SessionManager) Shutdown

func (sm *SessionManager) Shutdown()

Shutdown gracefully shuts down the session manager

func (*SessionManager) StartInteractiveSession

func (sm *SessionManager) StartInteractiveSession(sessionID string) error

StartInteractiveSession starts an interactive SSH terminal session

func (*SessionManager) TestNodeConnectivity

func (sm *SessionManager) TestNodeConnectivity(hostnames []string, _ time.Duration) map[string]error

TestNodeConnectivity tests SSH connectivity to multiple nodes

type SessionPersistence

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

SessionPersistence handles saving and loading SSH sessions

func NewSessionPersistence

func NewSessionPersistence(dataDir string) (*SessionPersistence, error)

NewSessionPersistence creates a new session persistence manager

func (*SessionPersistence) CleanupOldData

func (sp *SessionPersistence) CleanupOldData() error

CleanupOldData removes old session data files

func (*SessionPersistence) LoadSessionTags

func (sp *SessionPersistence) LoadSessionTags(sessionID string) (map[string]string, error)

LoadSessionTags loads user-defined tags for a session

func (*SessionPersistence) LoadSessions

func (sp *SessionPersistence) LoadSessions() ([]PersistentSession, error)

LoadSessions loads previously saved sessions from disk

func (*SessionPersistence) SaveSessionTags

func (sp *SessionPersistence) SaveSessionTags(sessionID string, tags map[string]string) error

SaveSessionTags saves user-defined tags for a session

func (*SessionPersistence) SaveSessions

func (sp *SessionPersistence) SaveSessions(sessions map[string]*SSHSession) error

SaveSessions saves all active sessions to disk

type SessionState

type SessionState int

SessionState represents the state of an SSH session

const (
	// SessionIdle is the idle session state.
	SessionIdle SessionState = iota
	// SessionConnecting is the connecting session state.
	SessionConnecting
	// SessionConnected is the connected session state.
	SessionConnected
	// SessionActive is the active session state.
	SessionActive
	// SessionDisconnected is the disconnected session state.
	SessionDisconnected
	// SessionError is the error session state.
	SessionError
)

func (SessionState) String

func (s SessionState) String() string

type Terminal

type Terminal interface {
	tview.Primitive
	SendCommand(command string) error
	Resize(width, height int) error
	Close() error
	IsActive() bool
	GetNodeID() string
}

Terminal represents an SSH terminal interface

type TerminalInfo

type TerminalInfo struct {
	NodeID       string
	Hostname     string
	Username     string
	IsActive     bool
	ConnectedAt  time.Time
	LastActivity time.Time
}

TerminalInfo contains information about a terminal

type TerminalManager

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

TerminalManager manages multiple SSH terminal sessions

func NewTerminalManager

func NewTerminalManager(app *tview.Application) *TerminalManager

NewTerminalManager creates a new terminal manager

func (*TerminalManager) AddTerminal

func (tm *TerminalManager) AddTerminal(nodeID string, terminal Terminal) error

AddTerminal adds a new terminal

func (*TerminalManager) BroadcastCommand

func (tm *TerminalManager) BroadcastCommand(command string) []error

BroadcastCommand sends a command to all active terminals

func (*TerminalManager) CloseAll

func (tm *TerminalManager) CloseAll()

CloseAll closes all terminals

func (*TerminalManager) GetActiveCount

func (tm *TerminalManager) GetActiveCount() int

GetActiveCount returns the number of active terminals

func (*TerminalManager) GetCurrentNodeID

func (tm *TerminalManager) GetCurrentNodeID() string

GetCurrentNodeID gets the current node ID

func (*TerminalManager) GetCurrentTerminal

func (tm *TerminalManager) GetCurrentTerminal() Terminal

GetCurrentTerminal gets the current terminal

func (*TerminalManager) GetTerminal

func (tm *TerminalManager) GetTerminal(nodeID string) (Terminal, error)

GetTerminal gets a terminal by node ID

func (*TerminalManager) GetTerminalInfos

func (tm *TerminalManager) GetTerminalInfos() []TerminalInfo

GetTerminalInfos returns information about all terminals

func (*TerminalManager) GetTerminalList

func (tm *TerminalManager) GetTerminalList() []string

GetTerminalList returns an ordered list of terminal node IDs

func (*TerminalManager) HasTerminal

func (tm *TerminalManager) HasTerminal(nodeID string) bool

HasTerminal checks if a terminal exists

func (*TerminalManager) NextTerminal

func (tm *TerminalManager) NextTerminal()

NextTerminal switches to the next terminal

func (*TerminalManager) PreviousTerminal

func (tm *TerminalManager) PreviousTerminal()

PreviousTerminal switches to the previous terminal

func (*TerminalManager) RemoveTerminal

func (tm *TerminalManager) RemoveTerminal(nodeID string) error

RemoveTerminal removes a terminal

func (*TerminalManager) ResizeTerminal

func (tm *TerminalManager) ResizeTerminal(nodeID string, width, height int) error

ResizeTerminal resizes a specific terminal

func (*TerminalManager) SendCommand

func (tm *TerminalManager) SendCommand(nodeID, command string) error

SendCommand sends a command to a specific terminal

func (*TerminalManager) SetMaxTerminals

func (tm *TerminalManager) SetMaxTerminals(maxVal int)

SetMaxTerminals sets the maximum number of terminals

func (*TerminalManager) SetOnSwitch

func (tm *TerminalManager) SetOnSwitch(callback func(nodeID string))

SetOnSwitch sets the callback for terminal switching

func (*TerminalManager) SwitchToTerminal

func (tm *TerminalManager) SwitchToTerminal(nodeID string) error

SwitchToTerminal switches to a specific terminal

type Tunnel added in v0.3.0

type Tunnel struct {
	LocalPort  int
	RemoteHost string
	RemotePort int
	Type       string // "local" or "remote"
	Active     bool
}

Tunnel represents an SSH port forwarding tunnel

Jump to

Keyboard shortcuts

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