SSH Factory

com.jscape.inet.ssh
Class SshTask

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

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. $

Message - 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 Message 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 and subsequent tasks by setting it's status to complete.
 void dataReceived(SshDataReceivedEvent event)
          Gets data received from Ssh server as fired by SshListener
 java.lang.String getCharacterSet()
          Gets the character set
 java.lang.String getCommand()
          Gets the command to execute
 boolean getDebug()
          Checks whether debugging is enabled for this task.
 java.io.PrintStream getDebugStream()
          Gets debug stream used when printing debug messages.
 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.
 int getRegexMatchLength()
          Gets the number of characters to read from the end of the response buffer when using regular expressions to detect start or end prompts.
 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 getStatusString(int status)
          Gets status string based on task status
 java.lang.String getTerminator()
          Gets line terminator used when sending commands.
 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 setCharacterSet(java.lang.String characterSet)
          Sets character set used when write data
 void setDebug(boolean debug)
          Enables/disables additional debugging for this task.
 void setDebugStream(java.io.PrintStream debugStream)
          Sets debug stream to be used when printing debug messages.
 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 setRegexMatchLength(int regexMatchLength)
          Sets the number of characters to read from the end of the response buffer when using regular expressions to detect start or end prompts.
 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.
 
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

setDebug

public void setDebug(boolean debug)
Enables/disables additional debugging for this task.

Parameters:
debug - true if debugging is enabled, false otherwise

setDebugStream

public void setDebugStream(java.io.PrintStream debugStream)
Sets debug stream to be used when printing debug messages. Default debug stream is System.out

Parameters:
debugStream - a PrintStream

getDebugStream

public java.io.PrintStream getDebugStream()
Gets debug stream used when printing debug messages. Default debug stream is System.out

Returns:
a PrintStream

getDebug

public boolean getDebug()
Checks whether debugging is enabled for this task.

Returns:
true if enabled, false otherwise

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 and subsequent tasks 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()

getStatusString

public java.lang.String getStatusString(int status)
Gets status string based on task status

Parameters:
status - the status code
Returns:
the status
See Also:
PENDING, ACTIVE, RUNNING, COMPLETE

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, Unknown 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

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

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
See Also:
setRegexMatchLength(int)

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
See Also:
setRegexMatchLength(int)

setRegexMatchLength

public void setRegexMatchLength(int regexMatchLength)
Sets the number of characters to read from the end of the response buffer when using regular expressions to detect start or end prompts. Default value is 20.

Parameters:
regexMatchLength -

getRegexMatchLength

public int getRegexMatchLength()
Gets the number of characters to read from the end of the response buffer when using regular expressions to detect start or end prompts. Default value is 20.

Returns:
match length

isTimedOut

public boolean isTimedOut()
Checks if task has exceeded timeout

Returns:
true if timeout exceeded, false otherwise

setCharacterSet

public void setCharacterSet(java.lang.String characterSet)
Sets character set used when write data

Parameters:
characterSet - the character set

getCharacterSet

public java.lang.String getCharacterSet()
Gets the character set

Returns:
The character set used

SSH Factory

Copyright © JSCAPE LLC. 1999-2011. All Rights Reserved