sshclient

package
v0.0.5 Latest Latest
Warning

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

Go to latest
Published: Nov 13, 2025 License: MIT Imports: 15 Imported by: 0

Documentation

Index

Constants

View Source
const (
	DefaultSSHPort    = "22"
	DefaultSSHUser    = "master"
	DefaultSudoKey    = "master"
	DefaultTimeout    = 30 * time.Second
	SudoPrompt        = "[sudo] password"
	PasswordPromptEnd = ": "
)
View Source
const KeyringServiceName = "sshx"

Variables

This section is empty.

Functions

func CloseIgnore

func CloseIgnore(errp *error, c io.Closer, ignore ...error)

CloseIgnore closes the closer and handles errors appropriately. It will ignore errors in the ignore list and merge other errors into errp. This is useful for defer statements where we want to handle close errors but allow certain expected errors to be silently ignored.

Usage example:

func handle(r io.ReadCloser) (err error) {
    defer CloseIgnore(&err, r, net.ErrClosed)
    // ... read ...
    return
}

func GetSudoPassword

func GetSudoPassword(key string) (string, error)

GetSudoPassword reads sudo password from system keyring (cross-platform support) macOS: Keychain, Linux: Secret Service (gnome-keyring/kwallet), Windows: Credential Manager

func ValidateCommand

func ValidateCommand(command string) error

ValidateCommand validates command safety

Types

type Config

type Config struct {
	Host     string
	Port     string
	User     string
	Password string
	KeyPath  string
	SudoKey  string
	Command  string
	Mode     string

	SafetyCheck bool
	Force       bool

	SftpAction string
	LocalPath  string
	RemotePath string

	PasswordAction string
	PasswordKey    string
	PasswordValue  string
}

Config represents SSH configuration properties for connecting to remote hosts.

type ConnectionPool

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

ConnectionPool manages SSH connections with pooling and health checks

func GetConnectionPool

func GetConnectionPool() *ConnectionPool

GetConnectionPool returns the global connection pool singleton

func NewConnectionPool

func NewConnectionPool() *ConnectionPool

NewConnectionPool creates a new connection pool

func (*ConnectionPool) Close

func (p *ConnectionPool) Close()

Close closes all connections in the pool

func (*ConnectionPool) GetConnection

func (p *ConnectionPool) GetConnection(config *Config) (*ssh.Client, error)

GetConnection retrieves or creates a connection from the pool

func (*ConnectionPool) ReleaseConnection

func (p *ConnectionPool) ReleaseConnection(config *Config)

ReleaseConnection releases a connection back to the pool

func (*ConnectionPool) Stats

func (p *ConnectionPool) Stats() map[string]interface{}

Stats returns connection pool statistics

type PooledConnection

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

PooledConnection represents a pooled SSH connection

type SSHClient

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

SSHClient wraps an ssh.Client with optional pooled and sftp helpers.

func NewSSHClient

func NewSSHClient(config *Config) (*SSHClient, error)

NewSSHClient 创建SSH客户端

func (*SSHClient) Close

func (c *SSHClient) Close() error

Close closes the connection (releases back to connection pool)

func (*SSHClient) Connect

func (c *SSHClient) Connect() error

Connect establishes an SSH connection (prefers using connection pool)

func (*SSHClient) ExecuteCommand

func (c *SSHClient) ExecuteCommand() (err error)

ExecuteCommand executes a command

func (*SSHClient) ExecuteCommandWithOutput

func (c *SSHClient) ExecuteCommandWithOutput() (output string, err error)

ExecuteCommandWithOutput executes a command and returns the output

func (*SSHClient) ExecuteScript

func (c *SSHClient) ExecuteScript(localScriptPath string) (output string, err error)

ExecuteScript executes a local script file 1. Upload script to remote temp directory 2. Add execute permission 3. Execute script 4. Clean up temp file

func (*SSHClient) ExecuteScriptWithArgs

func (c *SSHClient) ExecuteScriptWithArgs(localScriptPath string, args []string) (output string, err error)

ExecuteScriptWithArgs executes a script with arguments

func (*SSHClient) ExecuteSftp

func (c *SSHClient) ExecuteSftp() (err error)

ExecuteSftp executes SFTP operations

func (*SSHClient) ForceClose

func (c *SSHClient) ForceClose() error

ForceClose forcefully closes the connection (does not release back to pool)

Jump to

Keyboard shortcuts

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