SSH#
SSH transport: one reusable session per batch, resolved through ~/.ssh/config.
- class remote_compression.ssh.ExecResult(exit_status: int, stdout_tail: str = '', stderr_tail: str = '')[source]#
Outcome of a remote command.
- stdout_tail#
Last
TAIL_BYTESbytes of stdout, decoded with replacement.- Type:
- stderr_tail#
Last
TAIL_BYTESbytes of stderr, decoded with replacement.- Type:
- exception remote_compression.ssh.RemoteExecError[source]#
A remote command stopped producing output (inactivity timeout).
- exception remote_compression.ssh.SSHConnectionError[source]#
Host unreachable, authentication failure, host key mismatch, or connection lost for good.
- class remote_compression.ssh.SSHSession(hostname, config_path=None, attempts=3, connect_timeout=20.0, keepalive=30)[source]#
A reusable SSH connection (plus SFTP) to one destination.
Composition over inheritance: the session owns up to two
paramiko.SSHClient(an optional ProxyJump gateway and the destination) and a lazy SFTP channel. Use as a context manager; it returns itself.- Parameters:
hostname (
str) – Destination alias; extra parameters (HostName, User, Port, IdentityFile, ProxyJump) come from the openSSH config file.config_path (
str, optional) – Alternative openSSH config file.attempts (
int) – Connection attempts before giving up.connect_timeout (
float) – Timeout (seconds) applied to TCP, banner and auth phases.keepalive (
int) – Keepalive interval (seconds), applied to both transports.
- run(command, inactivity_timeout=300.0, on_stdout=None)[source]#
Execute a remote command, draining its output continuously.
Continuous draining is not cosmetic: paramiko’s flow-control window (~2 MB) fills up otherwise and the remote process blocks on write. The timeout is an inactivity timeout, not a total duration: any output (e.g. the ffmpeg
-progressstream) resets it.- Parameters:
- Returns:
Exit status and bounded output tails.
- Return type:
- Raises:
RemoteExecError – If the command stops producing output for inactivity_timeout seconds.
SSHConnectionError – If the connection drops during execution.
- property sftp#
lazy SFTP channel, reopened when dead.
- Raises:
SSHError – When the server refuses the SFTP subsystem (e.g. Synology DSM ships with the SFTP service disabled) — with the remedy spelled out.
SSHConnectionError – When the connection died.
- Type:
paramiko.SFTPClient
- remote_compression.ssh.TAIL_BYTES = 4096#
Bytes of output kept per channel when reporting a command result.
- remote_compression.ssh.is_localhost(hostname, config_path=None)[source]#
Tell whether an SSH destination is in fact this very machine.
- Parameters:
- Returns:
True when the effective HostName designates this machine. Guards: a ProxyJump or a non-standard port always means “remote” (a tunnel like
HostName localhost / Port 2222usually leads to another machine).- Return type:
Notes
Known false negative, by design: a DNS name resolving to the public IP of a NAT that loops back to this machine (hairpin) is treated as remote — the public address matches no local interface. Everything still works, only through a pointless SSH round-trip. When it matters, use
-D localor an alias whose HostName is the LAN name of the machine. Detecting this reliably would require comparing SSH host keys after connecting, which is not worth the machinery.