Telnet .NET Component
The Telnet .NET Component provides easy to use client implementations for Telnet, rlogin, rsh and rexec protocols.
 
Features
- Scripting API - Easy to use API providing automation of telnet
tasks.
- Regular expression support - Define complex tasks using regular expressions.
- All major components - Includes Telnet, Rexec, Rsh and Rlogin components.
- RFC compliant - Fully compliant with RFC 854 and RFC 855.
Code Example
The following C Sharp example demonstrates an interactive session using the Telnet .NET component.
/*
* TelnetExample.cs
*
* Copyright (c) 1999-2005 JSCAPE, LLC
* 1147 S. 53rd Pl., Mesa, Arizona, 85206, U.S.A.
* All rights reserved.
*
* This software is the confidential and proprietary information of
* JSCAPE ("Confidential Information"). You shall not disclose such
* Confidential Information and shall use it only in accordance with
* the terms of the license agreement you entered into with JSCAPE.
*/
using System;
using System.IO;
using System.Text;
using System.Threading;
using Jscape.Telnet;
namespace TelnetExample {
public class TelnetExample {
public bool connected = false;
public Telnet myTelnet = null;
public TelnetExample(string hostname){
// Instantiate Telnet
myTelnet = new Telnet(hostname);
// Subscribe to connection events
myTelnet.ConnectedEvent += new Telnet.ConnectedEventHandler(OnConnected);
myTelnet.DisconnectedEvent += new Telnet.DisconnectedEventHandler(OnDisconnected);
// Subscribe to option events. In this example, refuse all options.
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);
// Connect to telnet server
myTelnet.Connect();
this.Connected = true;
// 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;
}
}
// Disconnect from telnet server
myTelnet.Disconnect();
}
public bool Connected {
get {
return connected;
}
set {
connected = value;
}
}
public static void Main() {
Console.Write("Telnet server: ");
string hostname = "";
if ((hostname = Console.ReadLine()) != "") {
TelnetExample telnetexample = new TelnetExample(hostname);
}
}
public void OnConnected(object sender, TelnetConnectedEventArgs e) {
Console.WriteLine("Connected to {0}:{1}", e.Host, e.Port);
}
public void OnDisconnected(object sender, TelnetDisconnectedEventArgs e) {
this.Connected = false;
Console.WriteLine("Disconnected.");
}
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);
}
public void OnDataReceived(object sender, TelnetDataReceivedEventArgs e) {
Console.Write(myTelnet.Encoding.GetString(e.Data));
}
}
}
 
|
|