Telnet using C#
This article will demonstrate how using the Telnet Factory for
.NET component you can establish an interactive TELNET session with
a TELNET server. This article will also serve as a prerequisite
to an upcoming article titled Scripting
Telnet Sessions using C#, which will demonstrate how to automate
TELNET commands in a batch like process.
To see what else Telnet Factory for .NET has to offer Download
a FREE 30 day Telnet Factory for .NET Evaluation.
The Telnet Factory for .NET component provides a method for communicating
with a TELNET server. The process for establishing an interactive
session with a TELNET server using the Telnet Factory for .NET component
is as follows:
- Creating a new Telnet instance
- Subscribing to Telnet Events
- Establishing a connection
- Performing option negotiation
- Receiving data
- Sending data
- Releasing a connection
Each of these processes is described in the sections below.
Before creating a new Telnet instance, ensure that the Jscape.Telnet
scope is defined in your using statements, and that the Jscape.Telnet.dll
is referenced in your project. Refer to Getting Started in the Telnet
Factory for .NET Help for more information about adding the Jscape.Telnet.dll
reference to your projects.
Create a new Telnet instance providing the TELNET server hostname
as an argument.
Telnet myTelnet = new Telnet(hostname);
The Telnet instance MUST subscribe to the Telnet events prior to
invoking the Connect() method to ensure that all data sent by the
TELNET server is captured. See Receiving data later in this article
for more information about processing this event.
// Subscribe to connection events
myTelnet.ConnectedEvent += new Telnet.ConnectedEventHandler(OnConnected);
myTelnet.DisconnectedEvent += new Telnet.DisconnectedEventHandler(OnDisconnected);
// Subscribe to option events myTelnet.DoOptionEvent += new Telnet.DoOptionEventHandler(OnDoOption);
myTelnet.DontOptionEvent += new Telnet.DontOptionEventHandler(OnDontOption);
myTelnet.WillOptionEvent += new Telnet.WillOptionEventHandler(OnWillOption);
myTelnet.WontOptionEvent += new Telnet.WontOptionEventHandler(OnWontOption);
// Subscribe to data received event
myTelnet.DataReceivedEvent += new Telnet.DataReceivedEventHandler(OnDataReceived);
Now you can create the event handler methods you previously defined
when subscribing to the Telnet events. See Performing option negotiation
later in this article for more information about capturing and processing
option negotiation events.
public void OnConnected(object sender, TelnetConnectedEventArgs e) {
// Tell user we connected
Console.WriteLine("Connected to {0}:{1}", e.Host, e.Port);
}
public void OnDisconnected(object sender, TelnetDisconnectedEventArgs e) {
// Tell user we disconnected
this.Connected = false;
Console.WriteLine("Disconnected.");
}
Once a Telnet instance has been created and subscribed to the Telnet
events you may establish a connection to the TELNET server by invoking
the Connect() method.
myTelnet.Connect();
Upon establishing a connection to a TELNET server the process of
option negotiation automatically begins. Option negotiation is a
communications process for the TELNET client and the TELNET server
to come up with a set of agreed upon protocols. An example of option
negotiation is agreeing upon the terminal emulation (e.g. vt100,
xterm, dumb) to use during the TELNET session.
Option negotiation, as its name implies, is optional and may be
initiated by either the client or server. This does not mean, however,
that option negotiation may be ignored. For example, in the event
that the server attempts to perform option negotiation the client
must respond by either accepting or rejecting the option request.
Capturing option negotiation data from the TELNET server is accomplished
using the option event handlers subscribed to in the previous section.
In performing option negotiation there are four (4) TELNET protocol
commands that can be used by the client and server.
- DO OPTION - Requests to enable an option.
- DONT OPTION - Refuses offer to enable an option.
- WILL OPTION - Offers to enable an option.
- WONT OPTION - Refuses request to enable an option.
For the purposes of this article we will refuse all options both
requested and offered by the TELNET server. This, in effect, will
give us a basic TELNET client that is capable of exchanging data
with the TELNET server. In order to capture and refuse options requested
or offered by the TELNET server you will need to create the option
event handler methods as follows:
public void OnDoOption(object sender, TelnetDoOptionEventArgs e) {
myTelnet.SendWontOption(e.Option);
}
public void OnDontOption(object sender, TelnetDontOptionEventArgs e) {
myTelnet.SendDontOption(e.Option);
}
public void OnWillOption(object sender, TelnetWillOptionEventArgs e) {
myTelnet.SendDontOption(e.Option);
}
public void OnWontOption(object sender, TelnetWontOptionEventArgs e) {
myTelnet.SendWontOption(e.Option);
}
Once option negotiation has been completed, you may begin receiving
data sent by the TELNET server. To capture and display data received
from the TELNET server, create a data received event method as follows:
public void OnDataReceived(object sender, TelnetDataReceivedEventArgs e) {
// Write ascii characters of byte data received from TELNET server
Console.Write(Encoding.ASCII.GetString(e.Data));
}
To send data to the TELNET server you first obtain a TelnetOutputStream
from the Telnet instance. You can then send data to the TELNET server
from the console as follows:
// Begin reading and writing data to telnet server
TelnetOutputStream output = myTelnet.GetOutputStream();
string input = "";
while ((input = Console.ReadLine()) != null) {
if ((this.Connected) && (input != "exit")) {
((TelnetOutputStream) output).PrintLn(input);
} else {
break;
}
}
In the example above the input comes from the console and is redirected
to the TELNET server using the PrintLn() method. The TelnetOutputStream
class is used as it automatically appends a \r\n (carriage return
line feed) to the end of the data sent. This is required by the
TELNET server for it to know when it may begin processing the data
received.
To release an established connection simply invoke the Disconnect()
method as follows:
myTelnet.Disconnect();
The source code for this article is available for download and for
viewing.
View example source code
|