Overview
TheSSHClient class provides efficient command execution on guest VMs using persistent SSH connections. It uses paramiko to maintain a single TCP connection that is reused for all commands, eliminating the ~170ms overhead of forking a new ssh process per call.
The connection is established lazily on first use and automatically reconnects if the connection is lost.
Constructor
str
required
Guest IP address or hostname.
str
default:"root"
SSH username for authentication.
int
default:"22"
SSH port on the guest.
str | None
default:"None"
Optional path to an SSH private key file.
str | None
default:"None"
Optional password for authentication.
int
default:"10"
Seconds to wait for the TCP connection.
Literal["sh", "powershell", "cmd"]
default:"sh"
Login-shell flavor used to wrap commands when
run(..., shell="login") is called:"sh"(default) — POSIX guests. Wraps with$SHELL -lc <quoted>."powershell"— Windows guests. Wraps withpowershell.exe -NoProfile -EncodedCommand <base64>so the command bytes survive Windows OpenSSH’scmd.exelayer unchanged."cmd"— Windows guests where you wantcmd.exe /c "<command>"semantics instead. You are responsible forcmd.exequoting.
shell="raw" on run(...) bypasses the wrap entirely regardless of shell_kind.Properties
connected
bool
True if the connection is active, False otherwise.
Methods
run
str
required
Shell command to execute.
int
default:"30"
Maximum seconds to wait for the command to complete.
Literal['login', 'raw']
default:"login"
Command execution mode:
"login"(default): Run via guest login shell with environment variables loaded"raw": Execute command directly with no shell wrapping
CommandResult
Result object with:
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): Whether the command succeeded (exit_code == 0)output(str): Convenience alias for stripped stdout
ValueError: If command is emptyOperationTimeoutError: If the command exceeds timeoutSmolVMError: If the SSH connection cannot be established
wait_for_ssh
- TCP probe - Lightweight
socket.connect()calls (~1ms each) to detect when sshd is listening - Paramiko connect - Full SSH handshake + auth. The resulting connection is kept open for subsequent
run()calls
float
default:"60.0"
Maximum seconds to wait for SSH to become available.
float
default:"0.1"
Seconds between TCP probe attempts.
OperationTimeoutError
If SSH does not become available within timeout seconds.
close
Usage Patterns
Basic Command Execution
Error Handling
Using with SmolVM
Persistent Connection Benefits
Shell Modes
Performance
The persistent SSH connection provides significant performance benefits:- First command: ~100-200ms (establishes connection)
- Subsequent commands: ~10-50ms (reuses connection)
- Without persistence: ~170ms per command (fork + handshake overhead)
Related
- CommandResult - Result object returned by
run() - SmolVM - High-level VM facade that uses SSHClient internally
