Documentation
¶
Overview ¶
Package sshcommands provides SSH/SFTP operations (upload, list, delete, download, fetch host key) with parameter-based API, no config dependency.
Index ¶
- Constants
- Variables
- func AppendKnownHostsLine(knownHostsPath, hostToken, keyLine string, log Logger) error
- func AppendServerHostKey(opts *Opts, knownHostsPath string, log Logger) (written bool, err error)
- func Delete(opts *Opts, remoteDir string, names []string, log Logger) error
- func DialKnownHosts(opts *Opts, kh KnownHostsOptions, log Logger) (*ssh.Client, error)
- func DialWithHostKeyCallback(opts *Opts, hostKeyCallback ssh.HostKeyCallback, log Logger) (*ssh.Client, error)
- func Download(opts *Opts, pattern, destDir, remoteDir, aesPassword string, log Logger) ([]string, error)
- func FetchServerHostKey(opts *Opts) (keyLine string, err error)
- func FormatKnownHostsHostToken(host string, port int) string
- func HostKeyAlreadyPresent(currentValue, newKeyLine string) (bool, error)
- func MkdirAllRemote(client *ssh.Client, remotePath string, log Logger) error
- func NewSFTPClient(opts *Opts, log Logger) (*sftp.Client, *ssh.Client, error)
- func Sync(opts *Opts, localFiles []LocalFile, remoteDir, aesPassword string, log Logger) error
- func UploadFileIfNewer(client *ssh.Client, localPath, remotePath string, log Logger) error
- func ValidDownloadPattern(pattern string) bool
- type KnownHostsOptions
- type LocalFile
- type Logger
- type Opts
- type RemoteEntry
Constants ¶
const EncryptionOverhead = saltLen + nonceLen
EncryptionOverhead is the number of bytes added to an encrypted stream (salt + nonce).
Variables ¶
var ( // Version is the semantic-like version string of the library. // Format: Major.Minor.Patch.Build. Version = "0.2.4.2" // BuildTime is the timestamp of the build, typically injected via -ldflags // at build time. BuildTime = "2026-06-24 00:44:59" )
Functions ¶
func AppendKnownHostsLine ¶
AppendKnownHostsLine appends a host token and key line to a known_hosts file. Parent directories are created as needed.
func AppendServerHostKey ¶
AppendServerHostKey fetches the server host key and appends it to knownHostsPath when it is not already present. Returns written=true when a new line was added.
func DialKnownHosts ¶
DialKnownHosts connects using KnownHostsOptions.Path for host key verification. opts.HostKey is ignored. When FetchHostKey is set, the server key is fetched and appended when the file is missing or does not contain that key yet.
func DialWithHostKeyCallback ¶
func DialWithHostKeyCallback(opts *Opts, hostKeyCallback ssh.HostKeyCallback, log Logger) (*ssh.Client, error)
DialWithHostKeyCallback connects using a custom host key callback instead of opts.HostKey. opts.HostKey is ignored.
func Download ¶
func Download(opts *Opts, pattern, destDir, remoteDir, aesPassword string, log Logger) ([]string, error)
Download downloads files matching pattern from remoteDir into destDir. Pattern may be a literal filename or contain wildcards (*, ?). No path components. aesPassword empty = no decryption. Returns the list of local paths written.
func FetchServerHostKey ¶
FetchServerHostKey connects to the SSH server without host key verification, captures the server's host key, and returns it as a single line (key-type base64…). The client is closed immediately. Use for initial setup (e.g. -setup-ssh).
func FormatKnownHostsHostToken ¶
FormatKnownHostsHostToken returns the host token for a known_hosts entry.
func HostKeyAlreadyPresent ¶
HostKeyAlreadyPresent returns true if newKeyLine is already among the keys in currentValue (inline " || "-separated keys or path to a file with one key per line).
func MkdirAllRemote ¶
MkdirAllRemote creates remotePath and any missing parent directories via SFTP.
func NewSFTPClient ¶
NewSFTPClient returns an SFTP client; caller must call Close().
func Sync ¶
Sync uploads local files that are missing or newer on the remote, and deletes remote files that are not in localFiles. remoteDir is created if needed. aesPassword empty = no encryption.
func UploadFileIfNewer ¶
UploadFileIfNewer uploads localPath to remotePath when the local file is newer than the remote file or the remote file is missing. Remote parent directories are created as needed and the remote mtime is set to the local mtime.
func ValidDownloadPattern ¶
ValidDownloadPattern returns false if pattern contains path components (no /, \, ..).
Types ¶
type KnownHostsOptions ¶
type KnownHostsOptions struct {
// Path to the known_hosts file (required).
Path string
// FetchHostKey connects to the server, captures its host key, and appends
// it to Path when the file is missing or does not yet contain that key.
FetchHostKey bool
// TrustOnMismatch on host key mismatch fetches the current server key,
// appends it to Path, and retries the connection once.
TrustOnMismatch bool
}
KnownHostsOptions controls SSH connections that verify the server via an OpenSSH known_hosts file. opts.HostKey is ignored; Path is used instead.
type Opts ¶
type Opts struct {
Host string // SSH host
Port int // 0 => 22
User string
KeyFile string // path to private key (optional)
Password string // plaintext password (optional); at least KeyFile or Password required
HostKey string // path to known_hosts file or inline key(s), separated by " || "
}
Opts holds SSH/SFTP connection parameters.
type RemoteEntry ¶
RemoteEntry describes a remote file (name, modtime, size).