Overview
SSHClient manages persistent SSH connections to SmolVM guests using paramiko. It maintains a single TCP connection that is established lazily on first use and reused for all subsequent commands, eliminating the ~170ms overhead of forking a new ssh process per call.
Class Definition
Constructor
Parameters
Guest IP address or hostname.Cannot be empty.
SSH username for authentication.Cannot be empty.
SSH port on the guest.Must be between 1 and 65535.
Optional path to an SSH private key file for authentication.Takes precedence over password-based authentication.
Optional password for authentication.Used only if
key_path is not provided.Seconds to wait for the initial TCP connection.Must be >= 1.
Methods
wait_for_ssh
- TCP probe - Lightweight
socket.connect()calls (~1ms each) to detect when sshd is listening - Paramiko connect - Full SSH handshake + authentication. The connection is kept open for subsequent
run()calls.
Parameters
Maximum seconds to wait for SSH to become available.Must be > 0.
Seconds between TCP probe attempts during phase 1.
Raises
OperationTimeoutError: If SSH does not become available within timeout secondsValueError: If timeout is less than or equal to 0
Example
run
Parameters
Shell command to execute on the guest.Cannot be empty or whitespace-only.
Maximum seconds to wait for the command to complete.Must be >= 1.
Command execution mode:
"login"(default): Run via guest login shell (sources profile, bashrc, etc.)"raw": Execute command directly with no shell wrapping
Returns
Result object containing:
exit_code(int): Exit code of the command (0 = success)stdout(str): Standard output captured from the commandstderr(str): Standard error captured from the commandok(bool): Property returningTrueif exit_code == 0output(str): Property returningstdout.strip()
Raises
ValueError: If command is empty or timeout < 1OperationTimeoutError: If the command exceeds timeoutSmolVMError: If the SSH connection cannot be established
Example
close
Example
Properties
connected
True if a connection exists and its transport is active, False otherwise.
Example
Complete Example
Authentication Methods
Private Key Authentication
Password Authentication
Default Key/Agent Fallback
Performance Notes
- The persistent connection eliminates ~170ms of overhead per command compared to spawning
sshprocesses wait_for_ssh()uses fast TCP probes (~1ms each) before attempting the full SSH handshake- Connections are automatically re-established if they die between commands
- For maximum efficiency, keep the client alive and reuse it for multiple commands
Related
- CommandResult - Command execution result type
- SmolVM.run() - High-level command execution that uses SSHClient internally