Class SshExpect
- Namespace
- SshSharp
- Assembly
- SshSharp.dll
Drives an interactive program, as expect does: wait until its output shows a prompt or other text, then send the next input.
Wrap a shell session (StartShellAsync(SshSessionOptions?, CancellationToken), usually with a terminal) and use ExpectAsync(string, CancellationToken),
ReadLineAsync(CancellationToken) and SendLineAsync(string, CancellationToken); timeouts are CancellationTokens.
public sealed class SshExpect
- Inheritance
-
SshExpect
- Inherited Members
Remarks
This reads the output stream itself: do not read it elsewhere while using this. Output is decoded with Encoding, and a character split across two reads is decoded whole; invalid bytes become U+FFFD and are never dropped silently. Text after a match stays buffered for the next call. Without a terminal, standard error is a separate stream and is not searched.
A regular expression is tried against the output received so far, so a pattern that could also match a shorter prefix (\d+
against a number still arriving) may match early; anchor patterns to what follows them, such as a prompt's trailing $ .
Plain text is searched for only in the output that arrived since the last search (and the few characters before it), so waiting costs time in proportion to the output. A regular expression is tried against all the pending output after each read, up to MaxBufferLength: the program on the other side decides what that output is, so give patterns a MatchTimeout (a RegexMatchTimeoutException then ends the wait) and keep MaxBufferLength no larger than needed.
Constructors
- SshExpect(SshSession, Encoding?)
Drives
session: reads its standard output, writes its standard input.
- SshExpect(Stream, Stream, Encoding?)
Drives a program through its output and input streams.
Properties
- Encoding
The encoding of the program's output and input.
- MaxBufferLength
The most output kept while waiting for a match, in characters (1 MiB by default). When more arrives without a match, the oldest text is dropped, so a program that prints a lot without the expected text cannot exhaust memory.
- NewLine
What SendLineAsync(string, CancellationToken) ends a line with:
\nby default (a terminal turns it into what the program expects).
- Pending
Output received but not yet consumed by a match.
Methods
- ExpectAsync(string, CancellationToken)
Waits until the output contains
text(an ordinal match).
- ExpectAsync(Regex, CancellationToken)
Waits until the output matches
pattern.
- ReadLineAsync(CancellationToken)
Reads the next line of output: the text up to the next line feed, without it or a carriage return before it (a terminal ends lines with
\r\n). Once the output has ended, the text left without a line feed is the last line, and then null.
- SendAsync(string, CancellationToken)
Sends
textto the program as it is.
- SendLineAsync(string, CancellationToken)
Sends
linefollowed by NewLine, as typing it and pressing Enter does.