Telnet and Expect Readme

Telnet and related protocols for .NET 2.0.

Project Description

A .NET library providing Telnet connectivity, ANSI terminal support, and finally UNIX Expect-like scripting. The Telnet support includes option negotiation, and pluggable option handlers.

Sample code recreates the XP telnet command, shows a basic WinForms terminal, and also automated Expect scripting.


  • Can be used in two ways, interactive use, and automated scripting.
  • Gives direct access to the Telnet comms channel if necessary, escaping/encoding and decoding bytes as necessary.
    • Certain bytes are special in the telnet protocol and would be eaten if not escaped!
  • Handles Telnet Option Negotiation.
    • Can be configured to agree to particular options, or to disagree to all.
    • Negotiation can't be ignored or the peer will often sit waiting for the response and hang the connection.
  • Supports plug-in Option Handler classes. Raises events on the plug-ins on option change and on receipt of Sub-Negotiation commands. Four sample handlers are included:
    • One which provides Terminal-Type and Window-Size (NAWS),
    • Another which handles automatic log-in to Windows' telnet server (with NTLM),
    • One which lists the server's advertised Encryption modes,
    • And finally one which will request Status report from the server and log the response to the console.
    • The first sample uses both events, the others use only the sub-neg event.

Telnet Terminals

Sample code shows how to recreate the XP telnet command, and how to create a basic WinForms client.

The former is quite complete, the latter works in non-ANSI mode, but has little support for ANSI terminal mode where SetCursorPostion and EraseInLine etc commands are sent.


Both keyboard and screen support is included. On the keyboard side it converts key-presses into the corresponding ANSI/vt200 commands (for instance left-arrow into "ESC[D", and F6 into "ESC[17~"). On the screen side it provides parsing of received screen-control commands for instance cursor-up, set-colour etcm which are parsed from command of the form "ESC[(p)*Code", e.g "ESC[6,1H", or "ESC[K".

The support is used in the TelnetConsole sample program which behaves like the Windows telnet.exe console program. In particular logging into Window's telnet server works quite well -- small issues with scrolling. The parsed ANSI commands are used to send commands to the local console, e.g. "ESC[6,1H" into Console.SetCursorPosition(5,0), and "ESC[K" into clearing the current line.

Further implementation is possible. For instance shifted control keys, and more complete handling of screen control commands.


To enable scripting, UNIX Expect-like functionality is provided. This allows automated access to console applications, for instance to log into a remote device and reset a failed service.

An example script is the following:
    TcpClient cli = new TcpClient(hostName, TelnetStream.DefaultPort);
    TelnetStream telnet = new TelnetStream(cli.GetStream());
    // Don't want to see our keystrokes echoed back.
    telnet.SetRemoteMode(TelnetOption.Echo, false);
    m_xp = new Expector(telnet);
    m_xp.SendLine("net start");
    // If the service is running, it will be listed by "net start" and
    // the following line will match it.  If not we'll read to EoF and
    // an error will be produced.  (EndOfStreamException if the exit
    // command works, or IOException if timeout occurs).
    m_xp.Expect("   " + serviceDescr);
    // If we got here then the service is running!

See the Programmers Guide for more information.

Last edited Jul 4, 2010 at 6:55 PM by alanjmcf, version 11


prasanna_lanka Aug 20, 2014 at 11:41 AM 
Dear all,
I have downloaded the library and have no idea how to use it.
I want to login to unix, execute some commands and get the output to see if process are running.
Please someone help me as I am also new to c#.