Back to Microsandbox

SSH

docs/sdk/python/ssh.mdx

0.5.14.1 KB
Original Source

See SSH for usage flows.

Sandbox


ssh()

python
def ssh() -> SandboxSsh

Return the SSH namespace for this sandbox.

Returns

TypeDescription
SandboxSshSSH client and server helpers

SandboxSsh


connect()

python
async def connect(
    *,
    user: str = "root",
    term: str | None = None,
    sftp: bool = True,
) -> SshClient

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

Parameters

NameTypeDescription
userstrSSH login user
termstr | NoneTerminal name for interactive sessions
sftpboolEnable or disable SFTP on the internal server

Returns

TypeDescription
SshClientNative SSH client session

server()

python
async def server(
    *,
    host_key_path: str | os.PathLike[str] | None = None,
    authorized_keys_path: str | os.PathLike[str] | None = None,
    user: str | None = None,
    sftp: bool = True,
) -> SshServer

Prepare a reusable SSH server endpoint.

Parameters

NameTypeDescription
host_key_pathstr | os.PathLike[str] | NoneOverride the host private key path
authorized_keys_pathstr | os.PathLike[str] | NoneOverride the authorized-keys path
userstr | NoneOverride the guest user used for exec requests
sftpboolEnable or disable SFTP

Returns

TypeDescription
SshServerServer endpoint

SshClient


exec()

python
async def exec(command: str, *, tty: bool = False) -> SshOutput

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

Parameters

NameTypeDescription
commandstrCommand string sent through SSH
ttyboolRequest a PTY for the exec channel

Returns

TypeDescription
SshOutputCaptured output and status

attach()

python
async def attach(
    *,
    term: str | None = None,
    detach_keys: str | None = None,
) -> int

Attach the local terminal to an interactive SSH shell.

Parameters

NameTypeDescription
termstr | NoneTerminal name for the shell
detach_keysstr | NoneDetach key sequence

Returns

TypeDescription
intExit code

sftp()

python
async def sftp() -> SftpClient

Open an SFTP session over this SSH connection.

Returns

TypeDescription
SftpClientSFTP client session

close()

python
async def close() -> None

Close the native SSH client session.

SftpClient

MethodReturnsDescription
read(path)bytesRead a file into memory
write(path, data)NoneCreate or truncate a file
mkdir(path)NoneCreate a directory
remove_file(path)NoneRemove a file
remove_dir(path)NoneRemove an empty directory
rename(old_path, new_path)NoneRename a file or directory
real_path(path)strResolve a canonical path
read_link(path)strRead a symlink target
symlink(target, link_path)NoneCreate a symlink
close()NoneClose the SFTP session

SshServer


serve_stdio()

python
async def serve_stdio() -> None

Serve one SSH transport over stdin/stdout.


close()

python
def close() -> None

Release the prepared server endpoint.

Types

SshOutput

PropertyTypeDescription
statusintExit status code
successboolTrue if status is 0
stdout_textstrCaptured stdout decoded as UTF-8
stderr_textstrCaptured stderr decoded as UTF-8
stdout_bytesbytesCaptured stdout bytes
stderr_bytesbytesCaptured stderr bytes