SSH Factory

com.jscape.inet.ssh
Class SshTask

java.lang.Object
  extended bycom.jscape.inet.ssh.SshAdapter
      extended bycom.jscape.inet.ssh.SshTask
All Implemented Interfaces:
java.io.Serializable, SshListener

public class SshTask
extends SshAdapter
implements java.io.Serializable

The SshTask class represents a command to be executed in batch (non-interactive) mode on SSH server. In order to execute a command in batch mode the SshTask must know the following:

Start Prompt - The start prompt is the data that is sent back by the SSH server indicating that it is ready to execute the command. e.g. $

Command - The command is the command to be executed. e.g. ls -al

End Prompt - The end prompt is the data that is sent back by the SSH server indicating that it has completed execution of the command. In most cases this is the same as the Start Prompt. e.g. $

When creating a new SshTask you must at a MINIMUM provide both the Command and End Prompt. In most cases a Start Prompt should be provided although you may provide a null value in cases where you are not waiting for SSH server data but want the command to be executed immediately.

See Also:
Serialized Form

Field Summary
static int ACTIVE
          SshTask is active and waiting for start prompt to execute command
static int COMPLETE
          SshTask has finished execution of command and has received end prompt
static int PENDING
          SshTask is waiting to be activated
static int RUNNING
          SshTask has received start prompt and is currently executing command
 
Constructor Summary
SshTask(java.lang.String startPrompt, java.lang.String command, java.lang.String endPrompt)
          Constructs a new SshTask instance.
SshTask(java.lang.String name, java.lang.String startPrompt, java.lang.String command, java.lang.String endPrompt)
          Constructs a new SshTask instance.
 
Method Summary
 void cancel()
          Cancels this task by setting it's status to complete.
 void dataReceived(SshDataReceivedEvent event)
          Gets data received from Ssh server as fired by SshListener
 java.lang.String getCommand()
          Gets the command to execute
 java.lang.String getEndPrompt()
          Gets the end shell prompt this SshTask should wait for to indicate that this SshTask is complete.
 long getEndPromptTimeout()
          Gets the maximum timeout between the time the task is in running state and the expected end prompt is received.
 java.lang.String getName()
          Gets name of this SshTask as defined in constructor.
 SshTask getNextTask()
          Gets the next task executed after this SshTask.
 SshTask getPreviousTask()
          Gets the SshTask executed prior to this SshTask.
 java.lang.String getResponse()
          Gets response to command executed.
 SshScript getScript()
          Gets the SshScript this SshTask is bound to.
 java.lang.String getStartPrompt()
          Gets the shell prompt this SshTask should wait for before executing command.
 long getStartPromptTimeout()
          Gets the maximum timeout between the time the task is in active state and the expected start prompt is received.
 int getStatus()
          Gets completion status of this SshTask.
 java.lang.String getTerminator()
          Gets line terminator used when sending commands.
 long getTimeout()
          Deprecated. replaced by getEndPromptTimeout
 boolean isActive()
          Checks if this SshTask is active.
 boolean isComplete()
          Checks if this SshTask is complete.
 boolean isEndPromptRegex()
          Checks if end prompt is a regular expression
 boolean isPending()
          Checks if this SshTask is pending activation.
 boolean isRunning()
          Checks if this SshTask is running.
 boolean isStartPromptRegex()
          Specified if start prompt is a regular expression
 boolean isTimedOut()
          Checks if task has exceeded timeout
 void setEndPromptRegex(boolean b)
          Specifies if end prompt is a regular expression
 void setEndPromptTimeout(long endPromptTimeout)
          Sets the maximum timeout between the time the task is in running state and the expected end prompt is received.
 void setStartPromptRegex(boolean b)
          Specifies if start prompt is a regular expression
 void setStartPromptTimeout(long startPromptTimeout)
          Sets the maximum timeout between the time the task is in active state and the expected start prompt is received.
 void setTerminator(java.lang.String string)
          Sets line terminator used when sending commands.
 void setTimeout(long timeout)
          Deprecated. replaced by setEndPromptTimeout
 
Methods inherited from class com.jscape.inet.ssh.SshAdapter
connected, disconnected
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

PENDING

public static final int PENDING
SshTask is waiting to be activated

See Also:
Constant Field Values

ACTIVE

public static final int ACTIVE
SshTask is active and waiting for start prompt to execute command

See Also:
Constant Field Values

RUNNING

public static final int RUNNING
SshTask has received start prompt and is currently executing command

See Also:
Constant Field Values

COMPLETE

public static final int COMPLETE
SshTask has finished execution of command and has received end prompt

See Also:
Constant Field Values
Constructor Detail

SshTask

public SshTask(java.lang.String name,
               java.lang.String startPrompt,
               java.lang.String command,
               java.lang.String endPrompt)
Constructs a new SshTask instance.

Parameters:
name - a name to uniquely identify this SshTask.
startPrompt - the shell prompt this task should wait for before executing command.
command - the command to execute
endPrompt - the end shell prompt this task should wait for to indicate that this task is finished.

SshTask

public SshTask(java.lang.String startPrompt,
               java.lang.String command,
               java.lang.String endPrompt)
Constructs a new SshTask instance.

Parameters:
startPrompt - the start data this task should wait for before executing command.
command - the command to execute
endPrompt - the end data this task should wait for to indicate that this task is finished.
Method Detail

getNextTask

public SshTask getNextTask()
Gets the next task executed after this SshTask.

Returns:
the next SshTask or null if none found

getPreviousTask

public SshTask getPreviousTask()
Gets the SshTask executed prior to this SshTask.

Returns:
the previous SshTask or null if no previous SshTask found

getScript

public SshScript getScript()
Gets the SshScript this SshTask is bound to.

Returns:
a SshScript
See Also:
SshScript

dataReceived

public void dataReceived(SshDataReceivedEvent event)
Gets data received from Ssh server as fired by SshListener

Specified by:
dataReceived in interface SshListener
Overrides:
dataReceived in class SshAdapter
Parameters:
event - a SshDataReceivedEvent
See Also:
SshDataReceivedEvent, SshListener

cancel

public void cancel()
Cancels this task by setting it's status to complete.


getCommand

public java.lang.String getCommand()
Gets the command to execute

Returns:
the command to execute

getStartPrompt

public java.lang.String getStartPrompt()
Gets the shell prompt this SshTask should wait for before executing command.

Returns:
the start shell prompt

getEndPrompt

public java.lang.String getEndPrompt()
Gets the end shell prompt this SshTask should wait for to indicate that this SshTask is complete.

Returns:
the end shell prompt

getStatus

public int getStatus()
Gets completion status of this SshTask.

Returns:
the completion status of this SshTask
See Also:
PENDING, ACTIVE, RUNNING, COMPLETE

getResponse

public java.lang.String getResponse()
Gets response to command executed. Response will be empty if command has not executed.

Returns:
the response to command executed
See Also:
isComplete()

isPending

public boolean isPending()
Checks if this SshTask is pending activation.

Returns:
true if pending activation, false otherwise

isActive

public boolean isActive()
Checks if this SshTask is active.

Returns:
true if active, false otherwise

isRunning

public boolean isRunning()
Checks if this SshTask is running.

Returns:
true if running, false otherwise

isComplete

public boolean isComplete()
Checks if this SshTask is complete.

Returns:
true if complete, false otherwise

getName

public java.lang.String getName()
Gets name of this SshTask as defined in constructor.

Returns:
name of this SshTask, null if not defined.

getTerminator

public java.lang.String getTerminator()
Gets line terminator used when sending commands. Default is LF (Line Feed).

Returns:
line terminator

setTerminator

public void setTerminator(java.lang.String string)
Sets line terminator used when sending commands. Default is LF (Line Feed).

Parameters:
string - line terminator

getTimeout

public long getTimeout()
Deprecated. replaced by getEndPromptTimeout

Gets timeout used by task to determine when to abort task if end prompt is not received within specified timeout after task is started. Default timeout is 120000ms (2 minutes)

Returns:
the task timeout
See Also:
getEndPromptTimeout()

getEndPromptTimeout

public long getEndPromptTimeout()
Gets the maximum timeout between the time the task is in running state and the expected end prompt is received.

Returns:
the timeout in milliseconds

getStartPromptTimeout

public long getStartPromptTimeout()
Gets the maximum timeout between the time the task is in active state and the expected start prompt is received.

Returns:
the timeout in milliseconds

setTimeout

public void setTimeout(long timeout)
Deprecated. replaced by setEndPromptTimeout

Sets timeout used by task to determine when to abort task if end prompt is not received within specified timeout after task is started. Default timeout is 120000ms (2 minutes)

Parameters:
timeout - the task timeout
See Also:
setEndPromptTimeout(long)

setEndPromptTimeout

public void setEndPromptTimeout(long endPromptTimeout)
Sets the maximum timeout between the time the task is in running state and the expected end prompt is received.

Parameters:
endPromptTimeout - the timeout in milliseconds

setStartPromptTimeout

public void setStartPromptTimeout(long startPromptTimeout)
Sets the maximum timeout between the time the task is in active state and the expected start prompt is received.

Parameters:
startPromptTimeout - the timeout in milliseconds

isEndPromptRegex

public boolean isEndPromptRegex()
Checks if end prompt is a regular expression

Returns:
true if is a regular expression, false otherwise

isStartPromptRegex

public boolean isStartPromptRegex()
Specified if start prompt is a regular expression

Returns:
true if is a regular expression, false otherwise

setEndPromptRegex

public void setEndPromptRegex(boolean b)
                       throws SshException
Specifies if end prompt is a regular expression

Parameters:
b - true if end prompt is a regular expression, false otherwise
Throws:
SshException - if the end prompt is an invalid regular expression

setStartPromptRegex

public void setStartPromptRegex(boolean b)
                         throws SshException
Specifies if start prompt is a regular expression

Parameters:
b - true if start prompt is a regular expression, false otherwise
Throws:
SshException - if the start prompt is an invalid regular expression

isTimedOut

public boolean isTimedOut()
Checks if task has exceeded timeout

Returns:
true if timeout exceeded, false otherwise

SSH Factory

Copyright JSCAPE 2004 All Rights Reserved