Skip to content

janmz/sshcommands

Repository files navigation

sshcommands

Repository: github.com/janmz/sshcommands

Go library for SSH/SFTP operations with a parameter-based API: upload (sync), list, delete, download, and fetch server host key. No config struct dependency; all options are passed as parameters. Optional AES-256-CTR encryption for upload/download.

Donationware for CFI Kinderhilfe. License: MIT with attribution.

Installation

go get github.com/janmz/sshcommands

Usage

Connection options

Build Opts with SSH connection parameters:

opts := &sshcommands.Opts{
    Host:     "example.com",
    Port:     22,
    User:     "deploy",
    KeyFile:  "/home/user/.ssh/id_ed25519",  // or Password: "secret"
    HostKey:  "ed25519 AAAAC3...",            // or path to known_hosts file
}

Use at least KeyFile or Password for authentication. HostKey is required for verified connections (path to a file or inline key line; multiple keys separated by ||).

Sync (upload and prune)

Upload local files that are missing or newer on the remote; remove remote files that are not in the local list. Optional AES password for encryption.

localFiles := []sshcommands.LocalFile{
    {Name: "file.zip", Path: "/local/file.zip", ModTime: t, Size: 1024},
}
err := sshcommands.Sync(opts, localFiles, "/remote/dir", "", log)

List

List all non-directory entries in a remote directory.

entries, err := sshcommands.List(opts, "/remote/dir", log)
// entries: []RemoteEntry{Name, ModTime, Size}

Delete

Remove given file names from the remote directory.

err := sshcommands.Delete(opts, "/remote/dir", []string{"old.zip"}, log)

Download

Download files matching a pattern (literal name or wildcards *, ?) into a local directory. Optional AES password for decryption.

paths, err := sshcommands.Download(opts, "backup_*.zip", "/local/dir",
    "/remote/dir", aesPassword, log)
// paths: []string of written local paths

Fetch server host key

Connect without host key verification and return the server's key line (e.g. for initial setup):

keyLine, err := sshcommands.FetchServerHostKey(opts)
// keyLine: "ed25519 AAAAC3..."

Host key already present

Check if a key line is already among the configured keys (inline or file):

ok, err := sshcommands.HostKeyAlreadyPresent(currentHostKeyValue, newKeyLine)

Logger

All functions accept an optional Logger interface (Info, Warn with format strings). Pass nil to disable logging.

Encryption

When aesPassword is non-empty, Sync encrypts and Download decrypts using AES-256-CTR with PBKDF2-derived keys (salt + nonce prefix). Format is compatible with streams written by this package.

About

Go library for SSH/SFTP operations with a parameter-based API: upload (sync), list, delete, download, and fetch server host key. No config struct dependency; all options are passed as parameters. Optional AES-256-CTR encryption for upload/download.

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages