Table of Contents

Method RunCommandAsync

Namespace
SshSharp
Assembly
SshSharp.dll

RunCommandAsync(string, CancellationToken)

Runs command on the server and collects its output and exit status.

public Task<SshCommandResult> RunCommandAsync(string command, CancellationToken cancellationToken = default)

Parameters

command string
cancellationToken CancellationToken

Returns

Task<SshCommandResult>

Remarks

The command's standard input stays open and empty: a command that reads it waits until it is cancelled. To give it input, or to close its input, use RunCommandAsync(string, SshSessionOptions?, SshCommandInput?, CancellationToken).

Exceptions

SshException

The server refused the command or the connection failed.

InvalidOperationException

The output exceeded MaxCommandOutputBytes.

RunCommandAsync(string, SshSessionOptions?, SshCommandInput?, CancellationToken)

Runs command on the server with environment variables, a terminal or standard input, and collects its output and exit status. The session is set up as StartCommandAsync(string, SshSessionOptions?, CancellationToken) sets it up. standardInput is written while the output is collected, and end-of-file follows it (at once when there is none), so a command that reads its input to the end (cat, sh -s) finishes. A command that exits without reading all of its input is not an error.

public Task<SshCommandResult> RunCommandAsync(string command, SshSessionOptions? options, SshCommandInput? standardInput = null, CancellationToken cancellationToken = default)

Parameters

command string

The command line, run by the user's login shell.

options SshSessionOptions

Environment variables (sent as env requests, which servers accept only for the names they allow) and a terminal; null for neither. With a terminal, the server merges standard error into standard output, and the input goes through the terminal, which echoes it and does not pass end-of-file on to the program.

standardInput SshCommandInput

The command's standard input, or null for none (empty input).

cancellationToken CancellationToken

Cancels the command: the session is closed, and the server hangs up on the program.

Returns

Task<SshCommandResult>

Exceptions

SshException

The server refused the terminal or the command, or the connection failed.

InvalidOperationException

The output exceeded MaxCommandOutputBytes.