Table of Contents

Class RemoteProcess

Namespace
Tmds.Ssh
Assembly
Tmds.Ssh.dll

Represents a remote process.

public sealed class RemoteProcess : IDisposable
Inheritance
RemoteProcess
Implements
Inherited Members

Remarks

This class enables interacting with the remote process, such as reading its output streams, writing to its input stream, and retrieving the exit code.

If you won't be writing to the input stream, call WriteEof() to prevent blocking if the remote process prompts for input.

The class provides methods to read from standard output and standard error, and write to standard input using bytes, characters, strings, lines, or Streams.

When the output is read, you can call the GetExitCodeAsync(CancellationToken) method to retrieve the exit code. If there was any remaining output from the remote process it will be discarded.

Properties

ExecutionAborted

Gets a cancellation token that is canceled when operations that depend on the remote process should stop.

public CancellationToken ExecutionAborted { get; }

Property Value

CancellationToken

ExitCode

Returns the exit code of the process.

[Obsolete("Use GetExitCodeAsync instead.")]
public int ExitCode { get; }

Property Value

int

ExitSignal

Returns the signal that terminated the process when terminated by a signal.

[Obsolete("Use GetExitStatusAsync instead.")]
public string? ExitSignal { get; }

Property Value

string

HasTerminal

Returns whether the process was started with a terminal.

public bool HasTerminal { get; }

Property Value

bool

StandardInputStream

Returns a Stream for writing to the process standard input.

public Stream StandardInputStream { get; }

Property Value

Stream

Remarks

Disposing/Closing the Stream calls WriteEof().

StandardInputWriter

Returns a StreamWriter for writing to the process standard input.

public StreamWriter StandardInputWriter { get; }

Property Value

StreamWriter

Methods

Dispose()

Disposes the SSH channel that executes the process.

public void Dispose()

GetExitCodeAsync(CancellationToken)

Waits for the process to exit and returns the exit code. Any remaining output is discarded.

public ValueTask<int> GetExitCodeAsync(CancellationToken cancellationToken = default)

Parameters

cancellationToken CancellationToken

Token to cancel the operation.

Returns

ValueTask<int>

The exit code of the process.

GetExitStatusAsync(CancellationToken)

Waits for the process to exit and returns the exit code and signal. If there is pending output it is discarded.

public ValueTask<RemoteProcess.ExitStatus> GetExitStatusAsync(CancellationToken cancellationToken = default)

Parameters

cancellationToken CancellationToken

Token to cancel the operation.

Returns

ValueTask<RemoteProcess.ExitStatus>

The exit status containing the exit code and exit signal (if terminated by signal).

ReadAllLinesAsync(bool, bool, CancellationToken)

Reads all lines from the process output asynchronously.

public IAsyncEnumerable<(bool isError, string line)> ReadAllLinesAsync(bool readStdout = true, bool readStderr = true, CancellationToken cancellationToken = default)

Parameters

readStdout bool

Whether to read standard output.

readStderr bool

Whether to read standard error.

cancellationToken CancellationToken

Token to cancel the operation.

Returns

IAsyncEnumerable<(bool isError, string line)>

An async enumerable of lines with stdout or stderr indicator.

Remarks

After using this method, methods that read bytes or streams can no longer be used.

ReadAsStream(StderrHandler)

Reads the process output as a Stream.

public Stream ReadAsStream(StderrHandler stderrHandler)

Parameters

stderrHandler StderrHandler

StderrHandler for standard error output.

Returns

Stream

A Stream for reading standard output.

Remarks

After using this method, no other read methods can be used.

ReadAsStreamReader(StderrHandler, int)

Reads the process output as a StreamReader.

public StreamReader ReadAsStreamReader(StderrHandler stderrHandler, int bufferSize = -1)

Parameters

stderrHandler StderrHandler

StderrHandler for standard error output.

bufferSize int

Buffer size for the StreamReader.

Returns

StreamReader

A StreamReader for reading standard output.

Remarks

After using this method, no other read methods can be used.

ReadAsync(Memory<byte>?, Memory<byte>?, CancellationToken)

Reads process output as bytes.

public ValueTask<(bool isError, int bytesRead)> ReadAsync(Memory<byte>? stdoutBuffer, Memory<byte>? stderrBuffer, CancellationToken cancellationToken = default)

Parameters

stdoutBuffer Memory<byte>?

Buffer for standard output.

stderrBuffer Memory<byte>?

Buffer for standard error.

cancellationToken CancellationToken

Token to cancel the operation.

Returns

ValueTask<(bool isError, int bytesRead)>

A tuple indicating if data is from stdout or stderr and the amount of bytes read.

ReadAsync(Memory<char>?, Memory<char>?, CancellationToken)

Reads process output as characters.

public ValueTask<(bool isError, int bytesRead)> ReadAsync(Memory<char>? stdoutBuffer, Memory<char>? stderrBuffer, CancellationToken cancellationToken = default)

Parameters

stdoutBuffer Memory<char>?

Buffer for standard output.

stderrBuffer Memory<char>?

Buffer for standard error.

cancellationToken CancellationToken

Token to cancel the operation.

Returns

ValueTask<(bool isError, int bytesRead)>

A tuple indicating if data is from stdout or stderr and the amount of characters read.

Remarks

After using this method, methods that read bytes or streams can no longer be used.

ReadLineAsync(bool, bool, CancellationToken)

Reads a single line from the process output.

public ValueTask<(bool isError, string? line)> ReadLineAsync(bool readStdout = true, bool readStderr = true, CancellationToken cancellationToken = default)

Parameters

readStdout bool

Whether to read standard output.

readStderr bool

Whether to read standard error.

cancellationToken CancellationToken

Token to cancel the operation.

Returns

ValueTask<(bool isError, string line)>

A tuple indicating if the line is from stdout or stderr and the line text.

Remarks

After using this method, methods that read bytes or streams can no longer be used.

ReadToEndAsStringAsync(bool, bool, CancellationToken)

Reads all output until the process exits and returns it as strings.

public ValueTask<(string stdout, string stderr)> ReadToEndAsStringAsync(bool readStdout = true, bool readStderr = true, CancellationToken cancellationToken = default)

Parameters

readStdout bool

Whether to read standard output.

readStderr bool

Whether to read standard error.

cancellationToken CancellationToken

Token to cancel the operation.

Returns

ValueTask<(string stdout, string stderr)>

A tuple containing stdout and stderr as strings.

Remarks

After using this method, methods that read bytes or streams can no longer be used.

ReadToEndAsync(Func<Memory<byte>, object?, CancellationToken, ValueTask>?, object?, Func<Memory<byte>, object?, CancellationToken, ValueTask>?, object?, CancellationToken)

Reads all output until the process exits using custom handlers.

public ValueTask ReadToEndAsync(Func<Memory<byte>, object?, CancellationToken, ValueTask>? handleStdout, object? stdoutContext, Func<Memory<byte>, object?, CancellationToken, ValueTask>? handleStderr, object? stderrContext, CancellationToken cancellationToken = default)

Parameters

handleStdout Func<Memory<byte>, object, CancellationToken, ValueTask>

Handler for standard output data.

stdoutContext object

Context passed to the stdout handler.

handleStderr Func<Memory<byte>, object, CancellationToken, ValueTask>

Handler for standard error data.

stderrContext object

Context passed to the stderr handler.

cancellationToken CancellationToken

Token to cancel the operation.

Returns

ValueTask

ReadToEndAsync(Stream?, Stream?, CancellationToken)

Writes all output to Streams until the process exits.

public ValueTask ReadToEndAsync(Stream? stdoutStream, Stream? stderrStream, CancellationToken cancellationToken = default)

Parameters

stdoutStream Stream

Stream to write standard output to.

stderrStream Stream

Stream to write standard error to.

cancellationToken CancellationToken

Token to cancel the operation.

Returns

ValueTask

SendSignal(string)

Sends a signal to the process.

public bool SendSignal(string signalName)

Parameters

signalName string

The signal name (e.g., "TERM", "KILL").

Returns

bool

false if the remote process had already terminated; otherwise true.

SetTerminalSize(int, int)

Sets the terminal window size.

public bool SetTerminalSize(int width, int height)

Parameters

width int

The terminal width in characters.

height int

The terminal height in characters.

Returns

bool

false if the remote process had already terminated; otherwise true.

WaitForExitAsync(CancellationToken)

Waits for the process to exit.

[Obsolete("Use GetExitCodeAsync instead.")]
public ValueTask WaitForExitAsync(CancellationToken cancellationToken = default)

Parameters

cancellationToken CancellationToken

Token to cancel the operation.

Returns

ValueTask

WriteAsync(ReadOnlyMemory<byte>, CancellationToken)

Writes bytes to the process standard input.

public ValueTask WriteAsync(ReadOnlyMemory<byte> buffer, CancellationToken cancellationToken = default)

Parameters

buffer ReadOnlyMemory<byte>

The buffer to write.

cancellationToken CancellationToken

Token to cancel the operation.

Returns

ValueTask

WriteAsync(ReadOnlyMemory<char>, CancellationToken)

Writes characters to the process standard input.

public ValueTask WriteAsync(ReadOnlyMemory<char> buffer, CancellationToken cancellationToken = default)

Parameters

buffer ReadOnlyMemory<char>

The buffer to write.

cancellationToken CancellationToken

Token to cancel the operation.

Returns

ValueTask

WriteAsync(string, CancellationToken)

Writes a string to the process standard input.

public ValueTask WriteAsync(string value, CancellationToken cancellationToken = default)

Parameters

value string

The string to write.

cancellationToken CancellationToken

Token to cancel the operation.

Returns

ValueTask

WriteEof()

Writes end-of-file to the process standard input.

public void WriteEof()

WriteLineAsync(ReadOnlyMemory<char>, CancellationToken)

Writes a line of characters to the process standard input.

public ValueTask WriteLineAsync(ReadOnlyMemory<char> buffer = default, CancellationToken cancellationToken = default)

Parameters

buffer ReadOnlyMemory<char>

The buffer to write.

cancellationToken CancellationToken

Token to cancel the operation.

Returns

ValueTask

WriteLineAsync(string?, CancellationToken)

Writes a line of text to the process standard input.

public ValueTask WriteLineAsync(string? value, CancellationToken cancellationToken = default)

Parameters

value string

The string to write.

cancellationToken CancellationToken

Token to cancel the operation.

Returns

ValueTask