Hello,
I have found a solution to this, and in the interest of open source and since I have found the site very helpful in the past, here is the SSHWrapper which can be used, implementing a task timeout sleep inorder to terminate the read should it not return.
package com;
import com.jscape.inet.ssh.*;
import com.jscape.inet.ssh.util.SshParameters;
import java.io.*;
public class SSHWrapper extends SshAdapter {
private String sshHost;
private String sshUserName;
private String sshPassword;
private String sshPrompt;
private Ssh ssh = null;
private SshScript script = null;
private OutputStream output = null;
private static BufferedReader reader = null;
private int num_tasks;
private SshParameters sshParams;
private SshTask [] commandTask;
private int max_command_tasks;
private boolean force_immediate_execution;
private String response;
SSHWrapper(){
sshHost="";
sshUserName="";
sshPassword="";
sshPrompt="";
num_tasks=0;
max_command_tasks=500;
commandTask = new SshTask[max_command_tasks];
int i = 0;
force_immediate_execution=false;
response = "";
for ( i = 0 ; i < max_command_tasks; i++)
commandTask[i] = new SshTask("","","");
}
public String setup() throws IOException, SshException {
String return_value = "";
try {
System.out.println("Connecting to " + sshHost + " as " + sshUserName + "/" + sshPassword);
sshParams = new SshParameters(sshHost,sshUserName,sshPassword);
ssh = new Ssh(sshParams);
// register for events
ssh.addSshListener(this);
// create new script
script = new SshScript(ssh);
return_value = "Setup successful";
}catch( Exception ex){
return_value = "Connection failed with following exception : " + ex.getMessage();
return return_value;
}
return return_value;
}
public String build_script( ) throws IOException, SshException {
String return_value = "";
int i = 0;
try {
System.out.println("Building script for " + num_tasks + " tasks ");
for ( i = 0 ; i < num_tasks; i ++ )
script.addTask(commandTask[i]);
return_value = "Script built successfully : " + num_tasks + " in total ";
}catch( Exception ex){
return_value = "Script build failed with following exception : " + ex.getMessage();
return return_value;
}
return return_value;
}
public String add_command( String command) throws IOException, SshException {
String return_value = "";
try {
System.out.println("Adding the following command to script : " + command);
if ( ! force_immediate_execution )
commandTask[num_tasks] = new SshTask(sshPrompt,command,sshPrompt);
else
commandTask[num_tasks] = new SshTask(null,command,sshPrompt);
num_tasks++;
return_value = "Command added successfully : " + command + " / total_commands = " + num_tasks;
}catch( Exception ex){
return_value = "Addition of command failed with following exception : " + ex.getMessage();
return return_value;
}
return return_value;
}
public String connect_and_execute_script( ) throws IOException, SshException {
String return_value = "";
int i = 0;
try {
System.out.println("Connecting and executing script");
// connect to SSH server and execute script
ssh.connect();
// wait until last task is complete
while(i < num_tasks && !commandTask[num_tasks].isComplete()) {
try {
//System.out.println("Executing : " + commandTask[i].getCommand());
Thread.sleep(15000);
} catch(Exception e) {}
i++;
}
return_value = "Script executed successfully : " + num_tasks + " in total ";
// disconnect from server
ssh.disconnect();
}catch( Exception ex){
return_value = "Script execution failed at i = " + i + " : " + ex.getMessage();
return return_value;
}
return return_value;
}
/**
* Invoked when SSH connection is established.
*
* @param event a SshConnectedEvent
* @see SshConnectedEvent
* @see Ssh#connect
*/
public void connected(SshConnectedEvent event) {
System.out.println("Connected to host: " + event.getHost());
}
/**
* Invoked when SSH connection is released.
* Disconnect can occur in many circumstances including IOException during socket
* read/write or manually invoking the Ssh#disconnect
method.
*
* @param event a SshDisconnectedEvent
* @see SshDisconnectedEvent
* @see Ssh#disconnect
*/
public void disconnected(SshDisconnectedEvent event) {
System.out.println("Disconnected from host: " + event.getHost());
}
/**
* Invoked when data is received from Telnet server.
*
* @param event a SshDataReceivedEventode>
* @see SshDataReceivedEvent
*/
public void dataReceived(SshDataReceivedEvent event) {
response+=event.getData();
//System.out.print(response);
}
public String getsshHost ( ){
return sshHost;
}
public void setsshHost ( String Input_Value){
sshHost = Input_Value;
}
public String getsshUsreName ( ){
return sshUserName;
}
public void setsshUserName ( String Input_Value){
sshUserName = Input_Value;
}
public String getsshPassword ( ){
return sshPassword;
}
public void setsshPassword ( String Input_Value){
sshPassword = Input_Value;
}
public String getsshPrompt ( ){
return sshPrompt;
}
public void setsshPrompt ( String Input_Value){
sshPrompt = Input_Value;
}
public boolean getforce_immediate_execution ( ){
return force_immediate_execution;
}
public void setforce_immediate_execution ( boolean Input_Value){
force_immediate_execution = Input_Value;
}
public String getresponse ( ){
return response;
}
public void setresponse ( String Input_Value){
response = Input_Value;
}
}
Regards
Terry