Scripting Telnet Sessions using C#
This article will demonstrate how using the Telnet Factory for
.NET component you can establish a non-interactive Telnet session
with a TELNET server. For an introduction to using the TELNET protocol
with C# see the Telnet
using C# article.
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 class for communicating
with a TELNET server. The process for establishing a non-interactive
session with a TELNET server using the Telnet Factory for .NET component
is as follows:
- Creating a new Telnet Session instance
- Establishing a connection
- Sending commands to the server
- Terminating the Session
- 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 TelnetSession instance providing the TELNET server
hostname as an argument.
TelnetSession session = new TelnetSession("hostname");
Once a Telnet instance has been created you may establish a connection
to the TELNET server by invoking the Connect() method providing
the username and password as arguments.
session.Connect("username", "password");
Upon establishing a TelnetSession connection to a TELNET server
the process of option negotiation automatically begins. Refer to
the previous article, Telnet
using C#, for more information about option negotiation.
The TelnetSession refuses 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 non-interactively with
the Telnet server.
To send TelnetSession commands to the TELNET server you invoke
the Send() method passing the command to execute as follows:
session.Send("ls -al");
To send a command to the TELNET server without waiting for the
shell prompt, invoke the SendNoWait() method as follows:
session.SendNoWait("exit");
To release an established connection simply invoke the Disconnect()
method as follows:
session.Disconnect();
The source code for this article is available for download and for
viewing.
View example source
code
|