Table of Contents

Class ShellStream

Namespace
Renci.SshNet
Assembly
Renci.SshNet.dll

Contains operation for working with SSH Shell.

public class ShellStream : Stream, IDisposable, IAsyncDisposable
Inheritance
ShellStream
Implements
Inherited Members

Properties

CanRead

Gets a value indicating whether the current stream supports reading.

public override bool CanRead { get; }

Property Value

bool

true.

Remarks

It is safe to read from ShellStream even after disposal.

CanSeek

Gets a value indicating whether the current stream supports seeking.

public override bool CanSeek { get; }

Property Value

bool

false.

CanWrite

Gets a value indicating whether the current stream supports writing.

public override bool CanWrite { get; }

Property Value

bool

true if this stream has not been disposed and the underlying channel is still open, otherwise false.

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 CanWrite and the call to write.

DataAvailable

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

public bool DataAvailable { get; }

Property Value

bool

true if data is available to be read; otherwise, false.

Length

Gets the number of bytes currently available for reading.

public override long Length { get; }

Property Value

long

A long value representing the length of the stream in bytes.

Position

This property always returns 0, and throws 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 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 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 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 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 IAsyncResult that references the asynchronous operation.

Dispose(bool)

protected override void Dispose(bool disposing)

Parameters

disposing bool

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(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

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

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.

Flush()

public override void Flush()

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)

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

Parameters

buffer byte[]
offset int
count int

Returns

int

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 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 SeekOrigin indicating the reference point used to obtain the new position.

Returns

long

Never.

Exceptions

NotSupportedException

Always.

SetLength(long)

This method always throws NotSupportedException.

public override void SetLength(long value)

Parameters

value long

The desired length of the current stream in bytes.

Exceptions

NotSupportedException

Always.

Write(byte[], int, int)

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

Parameters

buffer byte[]
offset int
count int

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, Flush() is called after writing the data to the buffer.

Exceptions

ObjectDisposedException

The stream is 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. Flush() is called once the data is written.

Exceptions

ObjectDisposedException

The stream is closed.

Events

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>