Reach a running sandbox over SSH: open a native in-process SSH client, run exec requests, attach an interactive shell, transfer files over SFTP, or stand up a reusable SSH server endpoint. Requires the ssh feature. See SSH for usage flows.
microsandbox = { version = "0.5.8", features = ["ssh"] }
Typical flow
use microsandbox::Sandbox;let sb = Sandbox::builder("api") .image("python") .create() .await?;let client = sb.ssh().connect().await?; // 1. open an SSH clientlet out = client.exec("python -V").await?; // 2. run a commandprintln!("{}", String::from_utf8_lossy(&out.stdout));client.close().await?; // 3. close the session
let client = sb.ssh().connect().await?;let out = client.exec("uname -a").await?;
Connect a native in-process SSH client to this sandbox. Generates an ephemeral Ed25519 client and host key pair, stands up an internal server bound to a duplex stream, and authenticates over public key. Uses default client options (user root, terminal from $TERM, SFTP enabled).
let client = sb.ssh().connect_with(|opts| opts .user("app") .term("xterm-256color")).await?;
Connect a native in-process SSH client with custom options. The closure configures the login user, terminal name, and whether SFTP is enabled on the internal server.
let server = sb.ssh().server().await?;let (client_io, server_io) = tokio::io::duplex(64 * 1024);server.serve(server_io).await?;
Prepare a reusable SSH server endpoint for this sandbox. Loads or creates the host key and resolves authorized keys from the default authorized-keys file. The returned SshServer is cloneable and can serve many connections.
let server = sb.ssh().server_with(|opts| opts .authorized_key("ssh-ed25519 AAAAC3Nza...") .user("app") .sftp(false)).await?;
Prepare a server endpoint with custom host-key, authorization, guest-user, or SFTP options. If no authorized keys are configured, the default authorized-keys file is loaded; an empty key set is an error.
let out = client.exec("echo hello").await?;println!("exit {}: {}", out.status, String::from_utf8_lossy(&out.stdout));
Run an SSH exec request and collect stdout, stderr, and the exit status. The command is run through the sandbox’s configured shell (default /bin/sh -c). No PTY is requested.
let code = client.attach().await?;println!("shell exited with {code}");
Attach the local terminal to an interactive SSH shell. Requests a PTY sized to the current terminal, puts the terminal into raw mode, forwards keystrokes, relays window-resize events, and returns when the shell exits or the default detach key sequence is typed.
let sftp = client.sftp().await?;let mut file = sftp.create("/tmp/hello.txt").await?;
Open an SFTP client session over this SSH connection. Requests the sftp subsystem and returns a high-level SFTP session for reading, writing, and listing files inside the guest.
use microsandbox::SshStdioStream;let server = sb.ssh().server().await?;server.serve(SshStdioStream::new()).await?;
Create a stdio SSH transport stream backed by this process’s stdin and stdout. Implements AsyncRead and AsyncWrite, so it can be passed straight to serve to bridge an SSH connection over the parent process’s standard streams. Also available via Default.