Back to Microsandbox

SSH

docs/sdk/rust/ssh.mdx

0.5.15.9 KB
Original Source

See SSH for usage flows.

toml
microsandbox = { version = "0.4.6", features = ["ssh"] }

Sandbox


ssh()

rust
fn ssh(&self) -> SandboxSsh

Return the SSH namespace for this sandbox.

Returns

TypeDescription
SandboxSshSSH client and server helpers

SandboxSsh


connect()

rust
async fn connect(&self) -> MicrosandboxResult<SshClient>

Connect a native in-process SSH client to the sandbox.

Returns

TypeDescription
SshClientNative SSH client session

connect_with()

rust
async fn connect_with(
    &self,
    f: impl FnOnce(SshClientOptionsBuilder) -> SshClientOptionsBuilder,
) -> MicrosandboxResult<SshClient>

Connect a native SSH client with custom client options.

Parameters

NameTypeDescription
fFnOnce(SshClientOptionsBuilder) -> SshClientOptionsBuilderConfigure user, terminal, and SFTP support

Returns

TypeDescription
SshClientNative SSH client session

server()

rust
async fn server(&self) -> MicrosandboxResult<SshServer>

Prepare a reusable SSH server endpoint for this sandbox.

Returns

TypeDescription
SshServerReusable SSH server endpoint

server_with()

rust
async fn server_with(
    &self,
    f: impl FnOnce(SshServerOptionsBuilder) -> SshServerOptionsBuilder,
) -> MicrosandboxResult<SshServer>

Prepare a server endpoint with custom host-key, authorization, user, or SFTP options.

Parameters

NameTypeDescription
fFnOnce(SshServerOptionsBuilder) -> SshServerOptionsBuilderConfigure server options

Returns

TypeDescription
SshServerReusable SSH server endpoint

SshClient


exec()

rust
async fn exec(&self, command: impl Into<String>) -> MicrosandboxResult<SshOutput>

Run an SSH exec request and collect stdout, stderr, and exit status.

Parameters

NameTypeDescription
commandimpl Into<String>Command string sent through SSH

Returns

TypeDescription
SshOutputCaptured output and status

exec_with()

rust
async fn exec_with(
    &self,
    command: impl Into<String>,
    f: impl FnOnce(SshExecOptionsBuilder) -> SshExecOptionsBuilder,
) -> MicrosandboxResult<SshOutput>

Run an SSH exec request with options.

Parameters

NameTypeDescription
commandimpl Into<String>Command string sent through SSH
fFnOnce(SshExecOptionsBuilder) -> SshExecOptionsBuilderConfigure exec options

Returns

TypeDescription
SshOutputCaptured output and status

attach()

rust
async fn attach(&self) -> MicrosandboxResult<i32>

Attach the local terminal to an interactive SSH shell.

Returns

TypeDescription
i32Exit code

attach_with()

rust
async fn attach_with(
    &self,
    f: impl FnOnce(SshAttachOptionsBuilder) -> SshAttachOptionsBuilder,
) -> MicrosandboxResult<i32>

Attach with terminal options.

Parameters

NameTypeDescription
fFnOnce(SshAttachOptionsBuilder) -> SshAttachOptionsBuilderConfigure terminal and detach keys

Returns

TypeDescription
i32Exit code

sftp()

rust
async fn sftp(&self) -> MicrosandboxResult<SftpClient>

Open an SFTP session over this SSH connection.

Returns

TypeDescription
SftpClientSFTP client session

close()

rust
async fn close(self) -> MicrosandboxResult<()>

Close the native SSH client session.

SshServer


serve()

rust
async fn serve<S>(&self, stream: S) -> MicrosandboxResult<()>
where
    S: AsyncRead + AsyncWrite + Unpin + Send + 'static

Serve one SSH connection over an ordered duplex stream.

Parameters

NameTypeDescription
streamSOrdered duplex SSH transport

Types

SshOutput

FieldTypeDescription
statusi32Exit status code
stdoutBytesCaptured stdout bytes
stderrBytesCaptured stderr bytes

SftpClient

Alias for russh_sftp::client::SftpSession.

SshClientOptionsBuilder

MethodParametersDescription
user()impl Into<String>SSH login user. Defaults to root
term()impl Into<String>Terminal name for interactive sessions
sftp()boolEnable or disable SFTP on the internal server

SshExecOptionsBuilder

MethodParametersDescription
tty()boolRequest a PTY for the exec channel

SshAttachOptionsBuilder

MethodParametersDescription
term()impl Into<String>Terminal name for the shell
detach_keys()impl Into<String>Detach key sequence

SshServerOptionsBuilder

MethodParametersDescription
host_key_path()impl Into<PathBuf>Override the host private key path
host_key()PrivateKeyUse an in-memory host private key
authorized_keys_path()impl Into<PathBuf>Override the authorized-keys path
authorized_key()impl Into<String>Add one in-memory authorized public key
user()impl Into<String>Override the guest user used for exec requests
sftp()boolEnable or disable SFTP