Overview
This article will demonstrate how using the Telnet component present in both SSH Factory and Secure iNet Factory you can establish an interactive Telnet session with a Telnet server.
To see what else these products have to offer please download a FREE 30 day evaluation here: SSH Factory Evaluation and Secure iNet Factory Evaluation.
The Telnet client component provides a method for communicating with a Telnet server. The process for establishing an interactive session with a Telnet server using the Telnet component is as follows:
1. Creating a new Telnet instance
2. Implementing a TelnetListener
3. Registering a TelnetListener
4. Establishing a connection
5. Performing option negotiation
6. Receiving data
7. Sending data
8. Releasing a connection
Each of these processes is described in the sections below.
Creating a new Telnet instance
To create a new Telnet instance ensure that the com.jscape.inet.telnet package is included in your import statements and create a new Telnet instance providing the Telnet server hostname as an argument.
Telnet telnet = new Telnet(hostname);
Implementing a TelnetListener
The Telnet component is one of the few components which MUST have a registered event listener. The reason for this will be clear later. The following is a sample implementation of the TelnetListener interface. A TelnetListener instance must be created and registered with the Telnet instance prior to invoking the Telnet#connect method to ensure that all data sent by the Telnet server is captured.
public class MyTelnetListener implements TelnetListener {
public void connected(TelnetConnectedEvent event) {
}
public void disconnected(TelnetDisconnectedEvent event) {
}
public void doOption(DoOptionEvent event) {
}
public void dontOption(DontOptionEvent event) {
}
public void willOption(WillOptionEvent event) {
}
public void wontOption(WontOptionEvent event) {
}
public void doSubOption(DoSubOptionEvent event) {
}
public void dataReceived(TelnetDataReceivedEvent event) {
}
}
Registering a TelnetListener
To register a listener invoke the Telnet#addTelnetListener method providing a TelnetListener instance as its argument.
Telnet telnet = new Telnet(hostname);
MyTelnetListener listener = new MyTelnetListener();
telnet.addTelnetListener(listener);
Establishing a connection
Once a Telnet instance has been created and a TelnetListener registered you may establish a connection to the Telnet server by invoking the Telnet#connect method.
telnet.connect();
Performing option negotiation
Upon establishing a connection the process of option negotiation will begin. 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 in 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 TelnetListener class. In order to capture this data you must register an instance of the TelnetListener class with the Telnet instance as shown earlier.
In performing option negotiation there are four (5) Telnet protocol commands that can be used by the client and server.
1. DO OPTION - Requests to enable an option.
2. DONT OPTION - Refuses offer to enable an option.
3. WILL OPTION - Offers to enable an option.
4. WONT OPTION - Refuses request to enable an option.
5. SUB OPTION - For option subnegotiation
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 overload the TelnetListener#doOption and TelnetListener#willOption methods as follows:
public void doOption(DoOptionEvent event) {
// refuse any options requested by Telnet server
telnet.sendWontOption(event.getOption());
}
public void willOption(WillOptionEvent event) {
// refuse any options offered by Telnet server
telnet.sendDontOption(event.getOption());
}
Receiving data
Once option negotiation has been completed you may receive data sent by the Telnet server. To receive data, overload the TelnetListener#dataReceived method as follows:
public void dataReceived(TelnetDataReceivedEvent event) {
// print data received from Telnet server to console
System.out.println(event.getData());
}
Sending data
To send data to the Telnet server you will first obtain an OutputStream from the Telnet instance. Given this OutputStream you can then send data to the Telnet server as follows:
// sends all data entered at console to Telnet server
while ((input = reader.readLine()) != null) {
if (connected) {
((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 TelnetOutputStream#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.
Releasing a connection
To release an established connection simply invoke the Telnet#disconnect method as follows:
telnet.disconnect();
Example
Below is the fully functional source code for an interactive Telnet client written using the SSH Factory Telnet component.
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
import java.io.OutputStream;
import com.jscape.inet.telnet.DoOptionEvent;
import com.jscape.inet.telnet.Telnet;
import com.jscape.inet.telnet.TelnetAdapter;
import com.jscape.inet.telnet.TelnetConnectedEvent;
import com.jscape.inet.telnet.TelnetDataReceivedEvent;
import com.jscape.inet.telnet.TelnetDisconnectedEvent;
import com.jscape.inet.telnet.TelnetException;
import com.jscape.inet.telnet.TelnetOutputStream;
import com.jscape.inet.telnet.WillOptionEvent;
public class TelnetTutorial extends TelnetAdapter {
private Telnet telnet = null;
private OutputStream output = null;
private static BufferedReader reader = null;
private boolean connected = false;
public TelnetTutorial(String hostname)
throws IOException,
TelnetException
{
String input = null;
// create new Telnet instance
telnet = new Telnet(hostname);
// register this class as TelnetListener
telnet.addTelnetListener(this);
// establish Telnet connection
telnet.connect();
connected = true;
// get output stream
output = telnet.getOutputStream();
// sends all data entered at console to Telnet server
while ((input = reader.readLine()) != null) {
if (connected) {
((TelnetOutputStream) output).println(input);
} else {
break;
}
}
}
/** Invoked when Telnet socked is connected.
* @see TelnetConnectedEvent
* @see Telnet#connect
*/
public void connected(TelnetConnectedEvent event) {
System.out.println("Connected");
}
/**
* Invoked when Telnet socket is disconnected. Disconnect can
* occur in many circumstances including IOException during socket read/write.
* @see TelnetDisconnectedEvent
* @see Telnet#disconnect
*/
public void disconnected(TelnetDisconnectedEvent event) {
connected = false;
System.out.print("Disconnected. Press enter key to quit.");
}
/**
* Invoked when Telnet server requests that the Telnet client begin performing specified <code>TelnetOption</code>.
* @param event a <code>DoOptionEvent</code>
* @see DoOptionEvent
* @see TelnetOption
*/
public void doOption(DoOptionEvent event) {
// refuse any options requested by Telnet server
telnet.sendWontOption(event.getOption());
}
/**
* Invoked when Telnet server offers to begin performing specified <code>TelnetOption</code>.
* @param event a <code>WillOptionEvent</code>
* @see WillOptionEvent
* @see TelnetOption
*/
public void willOption(WillOptionEvent event) {
// refuse any options offered by Telnet server
telnet.sendDontOption(event.getOption());
}
/**
* Invoked when data is received from Telnet server.
* @param event a <code>TelnetDataReceivedEvent</code>
* @see TelnetDataReceivedEvent
*/
public void dataReceived(TelnetDataReceivedEvent event) {
// print data recevied from Telnet server to console
System.out.print(event.getData());
}
/**
* Main method for launching program
* @param args program arguments
*/
public static void main(String[] args) {
try {
reader = new BufferedReader(new InputStreamReader(System.in));
// prompt user for Telnet server hostname
System.out.print("Enter Telnet server hostname (e.g. 10.0.0.1): ");
String hostname = reader.readLine();
// create new TelnetTutorial instance
TelnetTutorial tutorial = new TelnetTutorial(hostname);
} catch (Exception e) {
e.printStackTrace(System.out);
}
}
}
Download the source code here: TelnetTutorial.java
Summary
In this article you learned how to establish an interactive Telnet session with a Telnet server using the Telnet component provided in both SSH Factory and Secure iNet Factory. The Telnet component in both SSH Factory and Secure iNet Factoryy makes this easy removing the complexities of the Telnet protocol.
References
Comments