ssh

package
v1.21.2 Latest Latest
Warning

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

Go to latest
Published: Aug 1, 2026 License: MIT Imports: 21 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AllHosts

func AllHosts() (string, error)

AllHosts returns all saved hosts as a formatted string for context injection.

func EnsureDir

func EnsureDir() error

EnsureDir ensures the SSH config directory exists.

func FindHost

func FindHost(query string) (*Host, []Host, error)

FindHost searches for a host by partial/fuzzy name match. It returns exact match first, then substring match, then address match.

func GenerateKeyPair

func GenerateKeyPair(name, keyType string, bits int) (pubPath, privPath string, err error)

GenerateKeyPair creates an SSH key pair and saves it to ~/.smara/ssh/keys/.

func RemoveHost

func RemoveHost(name string) error

RemoveHost removes a host by name.

func SaveHost

func SaveHost(host Host) error

SaveHost adds or updates a host in the config file.

Types

type Client

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

Client wraps an SSH client connection.

func Connect

func Connect(host *Host) (*Client, error)

Connect establishes an SSH connection to the given host.

func (*Client) Close

func (c *Client) Close() error

Close closes the SSH connection.

func (*Client) Exec

func (c *Client) Exec(command string) (stdout, stderr string, err error)

Exec runs a command on the remote host and returns stdout and stderr.

func (*Client) ExecStream

func (c *Client) ExecStream(command string, onOutput func(line string, isStderr bool)) error

ExecStream runs a command and streams output through a callback.

func (*Client) InteractiveSession

func (c *Client) InteractiveSession(stdin io.Reader, stdout, stderr io.Writer, termWidth, termHeight int) error

InteractiveSession starts an interactive PTY session.

type Host

type Host struct {
	Name     string `yaml:"name"`
	Address  string `yaml:"address"`
	Port     string `yaml:"port,omitempty"`
	User     string `yaml:"user"`
	KeyPath  string `yaml:"key_path,omitempty"`
	Password string `yaml:"password,omitempty"`
}

Host represents a saved SSH host configuration.

func GetHost

func GetHost(name string) (*Host, error)

GetHost retrieves a host by exact name.

func LoadHosts

func LoadHosts() ([]Host, error)

LoadHosts reads all saved SSH hosts.

type HostsFile

type HostsFile struct {
	Hosts []Host `yaml:"hosts"`
}

HostsFile is the top-level YAML structure.

type LogEntry

type LogEntry struct {
	ID        int64     `json:"id"`
	HostName  string    `json:"host_name"`
	Address   string    `json:"address"`
	Command   string    `json:"command"`
	Stdout    string    `json:"stdout"`
	Stderr    string    `json:"stderr"`
	Status    string    `json:"status"` // success / error
	Duration  int64     `json:"duration_ms"`
	CreatedAt time.Time `json:"created_at"`
}

LogEntry represents a single SSH execution log.

type Store

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

Store provides SQLite persistence for SSH logs.

func NewStore

func NewStore(dbPath string) (*Store, error)

NewStore creates a new SSH log store backed by SQLite.

func (*Store) Close

func (s *Store) Close() error

Close closes the database connection.

func (*Store) Init

func (s *Store) Init() error

Init creates the SSH tables if they don't exist.

func (*Store) ListLogs

func (s *Store) ListLogs(limit int) ([]LogEntry, error)

ListLogs returns recent SSH execution logs.

func (*Store) ListTransferLogs added in v1.13.0

func (s *Store) ListTransferLogs(limit int) ([]TransferLogEntry, error)

ListTransferLogs returns recent SSH file transfer logs.

func (*Store) SaveLog

func (s *Store) SaveLog(entry LogEntry) error

SaveLog inserts an execution log entry.

func (*Store) SaveTransferLog added in v1.13.0

func (s *Store) SaveTransferLog(entry TransferLogEntry) error

SaveTransferLog inserts a file transfer log entry.

type TransferConfig added in v1.13.0

type TransferConfig struct {
	Method        TransferMethod
	Recursive     bool
	PreservePerms bool
}

TransferConfig holds options for upload/download operations.

type TransferLogEntry added in v1.13.0

type TransferLogEntry struct {
	ID         int64     `json:"id"`
	HostName   string    `json:"host_name"`
	Address    string    `json:"address"`
	LocalPath  string    `json:"local_path"`
	RemotePath string    `json:"remote_path"`
	Direction  string    `json:"direction"` // upload / download
	Bytes      int64     `json:"bytes"`
	Method     string    `json:"method"` // sftp / scp
	Status     string    `json:"status"` // success / error
	ErrorMsg   string    `json:"error_msg"`
	Duration   int64     `json:"duration_ms"`
	CreatedAt  time.Time `json:"created_at"`
}

TransferLogEntry represents a single file transfer log.

type TransferMethod added in v1.13.0

type TransferMethod string

TransferMethod defines the protocol used for file transfer.

const (
	TransferMethodAuto TransferMethod = "auto"
	TransferMethodSFTP TransferMethod = "sftp"
	TransferMethodSCP  TransferMethod = "scp"
)

type TransferResult added in v1.13.0

type TransferResult struct {
	LocalPath  string
	RemotePath string
	Direction  string // upload / download
	Bytes      int64
	Duration   time.Duration
	Status     string // success / error
	Error      error
}

TransferResult summarizes a file transfer operation.

func DownloadDir added in v1.13.0

func DownloadDir(client *Client, remoteDir, localDir string, preserve bool) ([]TransferResult, error)

DownloadDir recursively downloads a directory via SFTP.

func DownloadFile added in v1.13.0

func DownloadFile(client *Client, remotePath, localPath string, preserve bool) (*TransferResult, error)

DownloadFile downloads a single file via SFTP.

func DownloadFileSCP added in v1.13.0

func DownloadFileSCP(client *Client, remotePath, localPath string, preserve bool) (*TransferResult, error)

SCP single-file download using ssh channel (scp -f).

func UploadDir added in v1.13.0

func UploadDir(client *Client, localDir, remoteDir string, preserve bool) ([]TransferResult, error)

UploadDir recursively uploads a directory via SFTP.

func UploadFile added in v1.13.0

func UploadFile(client *Client, localPath, remotePath string, preserve bool) (*TransferResult, error)

UploadFile uploads a single file via SFTP.

func UploadFileSCP added in v1.13.0

func UploadFileSCP(client *Client, localPath, remotePath string, preserve bool) (*TransferResult, error)

SCP single-file upload using ssh channel (scp -t).

Jump to

Keyboard shortcuts

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