Table of Contents

Class ShellStream

Namespace
Renci.SshNet
Assembly
Renci.SshNet.dll

Contains operation for working with SSH Shell.

public class ShellStream : Stream, IAsyncDisposable, IDisposable

Inheritance

Implements

Inherited Members

Properties

CanRead

Gets a value indicating whether the current stream supports reading.

public override bool CanRead { get; }

Property Value

bool

Remarks

It is safe to read from Renci.SshNet.ShellStream even after disposal.

CanSeek

Gets a value indicating whether the current stream supports seeking.

public override bool CanSeek { get; }

Property Value

bool

CanWrite

Gets a value indicating whether the current stream supports writing.

public override bool CanWrite { get; }

Property Value

bool

Remarks

A value of true does not necessarily mean a write will succeed. It is possible that the channel is closed and/or the stream is disposed by another thread between a call to Renci.SshNet.ShellStream.CanWrite and the call to write.

DataAvailable

Gets a value indicating whether data is available on the Renci.SshNet.ShellStream to be read.

public bool DataAvailable { get; }

Property Value

bool

Length

Gets the number of bytes currently available for reading.

public override long Length { get; }

Property Value

long

Position

This property always returns 0, and throws System.NotSupportedException when calling the setter.

public override long Position { get; set; }

Property Value

long

Exceptions

NotSupportedException

The setter is called.

Methods

BeginExpect(params ExpectAction[])

Begins the expect.

public IAsyncResult BeginExpect(params ExpectAction[] expectActions)

Parameters

expectActions ExpectAction[]

The expect actions.

Returns

IAsyncResult

An System.IAsyncResult that references the asynchronous operation.

BeginExpect(AsyncCallback?, params ExpectAction[])

Begins the expect.

public IAsyncResult BeginExpect(AsyncCallback? callback, params ExpectAction[] expectActions)

Parameters

callback AsyncCallback?

The callback.

expectActions ExpectAction[]

The expect actions.

Returns

IAsyncResult

An System.IAsyncResult that references the asynchronous operation.

BeginExpect(AsyncCallback?, object?, params ExpectAction[])

Begins the expect.

public IAsyncResult BeginExpect(AsyncCallback? callback, object? state, params ExpectAction[] expectActions)

Parameters

callback AsyncCallback?

The callback.

state object?

The state.

expectActions ExpectAction[]

The expect actions.

Returns

IAsyncResult

An System.IAsyncResult that references the asynchronous operation.

BeginExpect(TimeSpan, AsyncCallback?, object?, params ExpectAction[])

Begins the expect.

public IAsyncResult BeginExpect(TimeSpan timeout, AsyncCallback? callback, object? state, params ExpectAction[] expectActions)

Parameters

timeout TimeSpan

The timeout. Must non-negative or equal to -1 millisecond (for infinite timeout).

callback AsyncCallback?

The callback.

state object?

The state.

expectActions ExpectAction[]

The expect actions.

Returns

IAsyncResult

An System.IAsyncResult that references the asynchronous operation.

BeginExpect(TimeSpan, int, AsyncCallback?, object?, params ExpectAction[])

Begins the expect.

public IAsyncResult BeginExpect(TimeSpan timeout, int lookback, AsyncCallback? callback, object? state, params ExpectAction[] expectActions)

Parameters

timeout TimeSpan

The timeout. Must non-negative or equal to -1 millisecond (for infinite timeout).

lookback int

The amount of data to search through from the most recent data in the buffer, or -1 to always search the entire buffer.

callback AsyncCallback?

The callback.

state object?

The state.

expectActions ExpectAction[]

The expect actions.

Returns

IAsyncResult

An System.IAsyncResult that references the asynchronous operation.

Dispose(bool)

Releases the unmanaged resources used by the System.IO.Stream and optionally releases the managed resources.

protected override void Dispose(bool disposing)

Parameters

disposing bool

true to release both managed and unmanaged resources; false to release only unmanaged resources.

EndExpect(IAsyncResult)

Ends the execute.

public string? EndExpect(IAsyncResult asyncResult)

Parameters

asyncResult IAsyncResult

The async result.

Returns

string?

The text available in the shell up to and including the expected expression.

Expect(params ExpectAction[])

Expects the specified expression and performs action when one is found.

public void Expect(params ExpectAction[] expectActions)

Parameters

expectActions ExpectAction[]

The expected expressions and actions to perform.

Expect(TimeSpan, params ExpectAction[])

Expects the specified expression and performs action when one is found.

public void Expect(TimeSpan timeout, params ExpectAction[] expectActions)

Parameters

timeout TimeSpan

Time to wait for input. Must non-negative or equal to -1 millisecond (for infinite timeout).

expectActions ExpectAction[]

The expected expressions and actions to perform, if the specified time elapsed and expected condition have not met, that method will exit without executing any action.

Remarks

If a TimeSpan representing -1 millisecond is specified for the timeout parameter, this method blocks indefinitely until either the regex matches the data in the buffer, or the stream is closed (via disposal or via the underlying channel closing).

Expect(TimeSpan, int, params ExpectAction[])

Expects the specified expression and performs action when one is found.

public void Expect(TimeSpan timeout, int lookback, params ExpectAction[] expectActions)

Parameters

timeout TimeSpan

Time to wait for input. Must non-negative or equal to -1 millisecond (for infinite timeout).

lookback int

The amount of data to search through from the most recent data in the buffer, or -1 to always search the entire buffer.

expectActions ExpectAction[]

The expected expressions and actions to perform, if the specified time elapsed and expected condition have not met, that method will exit without executing any action.

Remarks

If a TimeSpan representing -1 millisecond is specified for the timeout parameter, this method blocks indefinitely until either the regex matches the data in the buffer, or the stream is closed (via disposal or via the underlying channel closing).

Use the lookback parameter to constrain the search space to a fixed-size rolling window at the end of the buffer. This can reduce the amount of work done in cases where lots of output from the shell is expected to be received before the matching expression is found.

Note: in situations with high volumes of data and a small value for lookback, some data may not be searched through. It is recommended to set lookback to a large enough value to be able to search all data as it comes in, but which still places a limit on the amount of work needed.

Expect(string)

Expects the expression specified by text.

public string? Expect(string text)

Parameters

text string

The text to expect.

Returns

string?

The text available in the shell up to and including the expected text, or null if the the stream is closed without a match.

Expect(string, TimeSpan, int)

Expects the expression specified by text.

public string? Expect(string text, TimeSpan timeout, int lookback = -1)

Parameters

text string

The text to expect.

timeout TimeSpan

Time to wait for input. Must non-negative or equal to -1 millisecond (for infinite timeout).

lookback int

The amount of data to search through from the most recent data in the buffer, or -1 to always search the entire buffer.

Returns

string?

The text available in the shell up to and including the expected expression, or null if the specified time has elapsed or the stream is closed without a match.

Remarks

If a TimeSpan representing -1 millisecond is specified for the timeout parameter, this method blocks indefinitely until either the regex matches the data in the buffer, or the stream is closed (via disposal or via the underlying channel closing).

Use the lookback parameter to constrain the search space to a fixed-size rolling window at the end of the buffer. This can reduce the amount of work done in cases where lots of output from the shell is expected to be received before the matching expression is found.

Note: in situations with high volumes of data and a small value for lookback, some data may not be searched through. It is recommended to set lookback to a large enough value to be able to search all data as it comes in, but which still places a limit on the amount of work needed.

Expect(Regex)

Expects the expression specified by regular expression.

public string? Expect(Regex regex)

Parameters

regex Regex

The regular expression to expect.

Returns

string?

The text available in the shell up to and including the expected expression, or null if the stream is closed without a match.

Expect(Regex, TimeSpan, int)

Expects the expression specified by regular expression.

public string? Expect(Regex regex, TimeSpan timeout, int lookback = -1)

Parameters

regex Regex

The regular expression to expect.

timeout TimeSpan

Time to wait for input. Must non-negative or equal to -1 millisecond (for infinite timeout).

lookback int

The amount of data to search through from the most recent data in the buffer, or -1 to always search the entire buffer.

Returns

string?

The text available in the shell up to and including the expected expression, or null if the specified timeout has elapsed or the stream is closed without a match.

Remarks

If a TimeSpan representing -1 millisecond is specified for the timeout parameter, this method blocks indefinitely until either the regex matches the data in the buffer, or the stream is closed (via disposal or via the underlying channel closing).

Use the lookback parameter to constrain the search space to a fixed-size rolling window at the end of the buffer. This can reduce the amount of work done in cases where lots of output from the shell is expected to be received before the matching expression is found.

Note: in situations with high volumes of data and a small value for lookback, some data may not be searched through. It is recommended to set lookback to a large enough value to be able to search all data as it comes in, but which still places a limit on the amount of work needed.

Flush()

When overridden in a derived class, clears all buffers for this stream and causes any buffered data to be written to the underlying device.

public override void Flush()

Exceptions

IOException

An I/O error occurs.

Read()

Reads all of the text currently available in the shell.

public string Read()

Returns

string

The text available in the shell.

Read(byte[], int, int)

When overridden in a derived class, reads a sequence of bytes from the current stream and advances the position within the stream by the number of bytes read.

public override int Read(byte[] buffer, int offset, int count)

Parameters

buffer byte[]

An array of bytes. When this method returns, the buffer contains the specified byte array with the values between offset and (offset + count - 1) replaced by the bytes read from the current source.

offset int

The zero-based byte offset in buffer at which to begin storing the data read from the current stream.

count int

The maximum number of bytes to be read from the current stream.

Returns

int

The total number of bytes read into the buffer. This can be less than the number of bytes requested if that many bytes are not currently available, or zero (0) if count is 0 or the end of the stream has been reached.

Exceptions

ArgumentException

The sum of offset and count is larger than the buffer length.

ArgumentNullException

buffer is null.

ArgumentOutOfRangeException

offset or count is negative.

IOException

An I/O error occurs.

NotSupportedException

The stream does not support reading.

ObjectDisposedException

Methods were called after the stream was closed.

ReadLine()

Reads the next line from the shell. If a line is not available it will block and wait for a new line.

public string? ReadLine()

Returns

string?

The line read from the shell.

Remarks

This method blocks indefinitely until either a line is available in the buffer, or the stream is closed (via disposal or via the underlying channel closing).

When the stream is closed and there are no more newlines in the buffer, this method returns the remaining data (if any) and then null indicating that no more data is in the buffer.

ReadLine(TimeSpan)

Reads a line from the shell. If line is not available it will block the execution and will wait for new line.

public string? ReadLine(TimeSpan timeout)

Parameters

timeout TimeSpan

Time to wait for input. Must non-negative or equal to -1 millisecond (for infinite timeout).

Returns

string?

The line read from the shell, or null when no input is received for the specified timeout.

Remarks

If a TimeSpan representing -1 millisecond is specified for the timeout parameter, this method blocks indefinitely until either a line is available in the buffer, or the stream is closed (via disposal or via the underlying channel closing).

When the stream is closed and there are no more newlines in the buffer, this method returns the remaining data (if any) and then null indicating that no more data is in the buffer.

Seek(long, SeekOrigin)

This method always throws System.NotSupportedException.

public override long Seek(long offset, SeekOrigin origin)

Parameters

offset long

A byte offset relative to the origin parameter.

origin SeekOrigin

A value of type System.IO.SeekOrigin indicating the reference point used to obtain the new position.

Returns

long

Never.

Exceptions

NotSupportedException

Always.

SetLength(long)

This method always throws System.NotSupportedException.

public override void SetLength(long value)

Parameters

value long

The desired length of the current stream in bytes.

Exceptions

NotSupportedException

Always.

Write(string?)

Writes the specified text to the shell.

public void Write(string? text)

Parameters

text string?

The text to be written to the shell.

Remarks

If text is null, nothing is written. Otherwise, Renci.SshNet.ShellStream.Flush is called after writing the data to the buffer.

Exceptions

ObjectDisposedException

The stream is closed.

Write(byte[], int, int)

When overridden in a derived class, writes a sequence of bytes to the current stream and advances the current position within this stream by the number of bytes written.

public override void Write(byte[] buffer, int offset, int count)

Parameters

buffer byte[]

An array of bytes. This method copies count bytes from buffer to the current stream.

offset int

The zero-based byte offset in buffer at which to begin copying bytes to the current stream.

count int

The number of bytes to be written to the current stream.

Exceptions

ArgumentException

The sum of offset and count is greater than the buffer length.

ArgumentNullException

buffer is null.

ArgumentOutOfRangeException

offset or count is negative.

IOException

An I/O error occurred, such as the specified file cannot be found.

NotSupportedException

The stream does not support writing.

ObjectDisposedException

System.IO.Stream.Write(System.Byte[],System.Int32,System.Int32) was called after the stream was closed.

WriteLine(string)

Writes the line to the shell.

public void WriteLine(string line)

Parameters

line string

The line to be written to the shell.

Remarks

If line is null, only the line terminator is written. Renci.SshNet.ShellStream.Flush is called once the data is written.

Exceptions

ObjectDisposedException

The stream is closed.

Closed

Occurs when the channel was closed.

public event EventHandler<EventArgs>? Closed

Event Type

EventHandler<EventArgs>?

DataReceived

Occurs when data was received.

public event EventHandler<ShellDataEventArgs>? DataReceived

Event Type

EventHandler<ShellDataEventArgs>?

ErrorOccurred

Occurs when an error occurred.

public event EventHandler<ExceptionEventArgs>? ErrorOccurred

Event Type

EventHandler<ExceptionEventArgs>?