Working with Telnet from Sahi
This section describes how to interact with Telnet server from Sahi.
Download the following jar and copy it to sahi/userdata/extlib folder (create extlib folder if not already present).
- commons-net-3.8.0.jar (Download https://dlcdn.apache.org//commons/net/binaries/commons-net-3.8.0-bin.zip, unzip it and copy only commons-net-3.8.0.jar)
Refer the functions below. Functions
connect
,
receive
, and
send
are wrapper functions written to interact with the telnet server. Add these functions to your Sahi script. Also, refer the sample script below.
// Wrapper functions
/*
* Call this function for connecting to the telnet server.
*/
function connect($host, $port)
{
var $telnet = new org.apache.commons.net.telnet.TelnetClient(); // Access telnet client
$telnet.connect($host, $port);
if( ! $telnet.isConnected() )
{
_log("Unable to connect to telnet server.");
}
return $telnet;
}
/*
* Call this function to receive data from the telnet server.
* In other words, whenever you expect the telnet server would send data, this function must be called.
*/
function receive($telnet)
{
try
{
var $sb = new java.lang.StringBuffer();
var $buffer = java.lang.reflect.Array.newInstance(java.lang.Byte.TYPE, 4096);
var $length = 0;
java.lang.Thread.sleep(800); // Alternatively, we can use _wait(800)
while ( ($length = $telnet.getInputStream().read($buffer)) > 0 )
{
$sb.append(new java.lang.String($buffer, 0, $length));
java.lang.Thread.sleep(800); // Alternatively use _wait(800)
if ( $telnet.getInputStream().available() == 0 )
{
break;
}
}
return $sb.toString() ;
}
catch ($e)
{
_log("Error while receiving data from the telnet server.");
_logExceptionAsFailure($e); // Logs the exception, and fails.
}
return "";
}
/*
* Call this function for sending data to the telnet server.
*/
function send($telnet, $data)
{
try
{
$telnet.getOutputStream().write(new java.lang.String($data).getBytes());
$telnet.getOutputStream().flush();
}
catch ($e)
{
_log("Error while sending data to the telnet server.");
_logExceptionAsFailure($e); // Logs the exception, and fails.
}
}
// Below is a sample script that employs above wrapper functions to interact with the telnet server.
var $telnet = connect("myserver.example.com", 8023); // Connect to the telnet server.
receive($telnet);
send($telnet, "0");
receive($telnet);
send($telnet, "1");
receive($telnet);
send($telnet, "username\r\n"); // Send User Name and enter key
receive($telnet);
send($telnet, "password\r\n"); // Send Password and enter key
var $str = receive($telnet);
_log($str);
$telnet.disconnect();
- These wrapper functions employ a java class 'org.apache.commons.net.telnet.TelnetClient' to communicate with the telnet server. Sahi itself does not do much here, except calling the correct APIs. If you run into any trouble, have a look at the documentation for Apache Commons Net.
- Telnet protocol provides bidirectional communication. Whenever you expect the telnet server would send data, the function
receive
must be called. It will wait and collect all the data sent by the server. Function send
must be called only at a time when the server is not sending any data.