remote

package
v0.2.26 Latest Latest
Warning

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

Go to latest
Published: Apr 18, 2026 License: MIT Imports: 19 Imported by: 0

Documentation

Overview

Package remote provides SSH exec + SFTP file ops against source/target hosts.

Index

Constants

This section is empty.

Variables

View Source
var ErrHostUnknown = errors.New("host not in known_hosts")

ErrHostUnknown signals that the remote host is not present in the known_hosts file. It is safe for callers to run `ssh-keyscan` and append the result; the server is not presenting a key that conflicts with a previously-recorded one.

A MISMATCHED host key (i.e. a MITM or server re-key) returns a different error and is NOT wrapped by ErrHostUnknown — callers must not auto-trust in that case.

Functions

func OpenDB

func OpenDB(d config.DB) (*sql.DB, error)

OpenDB opens a *sql.DB for the given config.DB and pings it with a 5s timeout. The returned DB uses the driver name compatible with the dialect.

Types

type Client

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

func Dial

func Dial(c *config.SSH) (*Client, error)

Dial opens an SSH connection with the settings from config.SSH. The caller must Close() when done.

Host key verification order:

  1. If HostKeyFingerprint is set, the advertised key's SHA256 fingerprint must match.
  2. Otherwise KnownHosts (defaulting to ~/.ssh/known_hosts) is consulted via golang.org/x/crypto/ssh/knownhosts.

There is intentionally no "trust on first use" fallback — if neither mechanism can authenticate the host, Dial returns an error.

func (*Client) Close

func (c *Client) Close() error

func (*Client) DirSizeBytes

func (c *Client) DirSizeBytes(path string) (uint64, error)

DirSizeBytes returns the size of a directory tree on the remote. Relies on `du -sb PATH`.

func (*Client) DiskFreeBytes

func (c *Client) DiskFreeBytes(path string) (uint64, error)

DiskFreeBytes returns free bytes on the filesystem holding path. Relies on `df -B1 --output=avail PATH | tail -1`.

func (*Client) Exists

func (c *Client) Exists(path string) (bool, error)

Exists returns true if path exists on the remote.

func (*Client) FetchFile

func (c *Client) FetchFile(remotePath, localPath string) (LocalFetchResult, error)

FetchFile downloads remotePath to localPath and returns how the transfer was satisfied. When the result is ResultSymlinked the caller MUST NOT delete the source file while localPath is still in use, or the symlink becomes dangling.

func (*Client) ReadFile

func (c *Client) ReadFile(path string) ([]byte, error)

ReadFile reads a remote file fully into memory. Use only for small files (app.ini, etc.); use OpenRemote for large files.

func (*Client) Run

func (c *Client) Run(cmd string) ([]byte, error)

Run executes cmd and returns combined stdout+stderr.

func (*Client) RunStream

func (c *Client) RunStream(cmd string, w io.Writer) error

RunStream executes cmd streaming stdout/stderr to w.

func (*Client) WriteFile

func (c *Client) WriteFile(path string, data []byte, mode os.FileMode) error

WriteFile writes data to a remote path with the given mode, creating the file if needed (truncates existing content).

type LocalFetchResult added in v0.2.10

type LocalFetchResult int

LocalFetchResult describes how FetchFile satisfied a same-host transfer. Callers use ResultSymlinked / ResultHardLinked to decide whether deleting the source file is safe — hard links keep the inode alive; symlinks break.

const (
	ResultCopied     LocalFetchResult = iota // bytes were actually moved
	ResultHardLinked                         // new directory entry, same inode
	ResultSymlinked                          // new symlink; source must NOT be deleted
	ResultRemote                             // SFTP was used (truly remote source)
)

Jump to

Keyboard shortcuts

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