|
Secure iNet Factory | |||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectcom.jscape.inet.ssh.SshSession
public class SshSession
Convenience class for automating communications with a SSH server. Example:
String shellPrompt = "$"; // creates new SshShession instance providing hostname, username and password arguments SshSession session = new SshSession(hostname,username,password); // set expected shell prompt from SSH server session.setShellPrompt(shellPrompt); // establish connection session.connect(); // send command to SSH shell and wait for shell prompt session.send("cd /user/logs"); // send command to SSH shell and wait for shell prompt session.send("rm *.log"); // send command to SSH shell and DO NOT wait for shell prompt session.sendNoWait("exit"); // close connection with SSH server session.disconnect();
Constructor Summary | |
---|---|
SshSession(SshParameters sshParams)
Creates a new SshSession instance. |
|
SshSession(java.lang.String hostname,
int port,
java.lang.String username,
java.lang.String password)
Creates a new SshSession instance. |
|
SshSession(java.lang.String hostname,
java.lang.String username,
java.lang.String password)
Creates a new SshSession instance. |
Method Summary | |
---|---|
void |
addSshListener(SshListener listener)
Add Ssh event listener. |
void |
connect()
Establishes connection with SSH server and performs login. |
void |
connect(int timeout)
Establishes connection with SSH server and performs login |
void |
debug(java.lang.String msg)
Sends debug message to debug stream. |
void |
disconnect()
Disconnects from SSH server. |
java.lang.String |
getCharacterSet()
Gets the character set |
java.lang.String |
getCommandTerminator()
Gets terminator used when sending data to SSH server. |
boolean |
getDebug()
Checks if debugging is enabled. |
java.io.PrintStream |
getDebugStream()
Gets PrintStream used in outputting debug information. |
boolean |
getEcho()
Returns echo flag. |
int |
getPollingInterval()
Gets the polling interval used when checking for completion of commands sent using send or sendWait methods. |
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 |
getShellPrompt()
Gets shell prompt to wait for before sending commands. |
Ssh |
getSsh()
Gets underlying Ssh instance. |
void |
interrupt()
Interrupts any running command waiting for an expected prompt. |
void |
pause(long timeout)
Pauses current session for specified timeout |
void |
removeSshListener(SshListener listener)
Remove Ssh event listener. |
java.lang.String |
send(java.lang.String command)
Sends command to SSH host terminated by command terminator. |
java.lang.String |
send(java.lang.String command,
long timeout)
Sends command to SSH host terminated by command terminator. |
java.lang.String |
send(java.lang.String command,
java.lang.String prompt)
Deprecated. replaced by sendWait(String, String) method |
void |
sendNoWait(java.lang.String command)
Sends command to SSH host terminated by command terminator. |
void |
sendRaw(java.lang.String data)
Sends data to SSH host without command terminator. |
java.lang.String |
sendWait(java.lang.String command,
java.lang.String prompt)
Sends command to SSH host terminated by command terminator. |
java.lang.String |
sendWait(java.lang.String command,
java.lang.String prompt,
boolean regex)
Sends command to SSH host terminated by command terminator. |
java.lang.String |
sendWait(java.lang.String command,
java.lang.String prompt,
boolean regex,
long timeout)
Sends command to SSH host terminated by command terminator. |
java.lang.String |
sendWait(java.lang.String command,
java.lang.String prompt,
long timeout)
Sends command to SSH host terminated by command terminator. |
void |
setCharacterSet(java.lang.String characterSet)
Sets the session character set |
void |
setCommandTerminator(java.lang.String string)
Sets terminator used when sending data to SSH server. |
void |
setDebug(boolean b)
Enables or disables debugging information. |
void |
setDebugStream(java.io.PrintStream stream)
Sets PrintStream used in outputting debug information. |
void |
setEcho(boolean echo)
Sets server echo flag for the next connection. |
void |
setPollingInterval(int pollingInterval)
Sets the polling interval used when checking for completion of commands sent using send or sendWait methods. |
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 |
setShellPrompt(java.lang.String string)
Sets shell prompt to wait for before sending commands. |
void |
setShellPrompt(java.lang.String string,
boolean regex)
Sets shell prompt to wait for before sending commands. |
void |
setSsh(Ssh ssh)
Sets the underlying Ssh instance. |
Methods inherited from class java.lang.Object |
---|
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
---|
public SshSession(SshParameters sshParams) throws SshException
sshParams
- an SshParameters instance
SshException
- if an SSH related exception occurspublic SshSession(java.lang.String hostname, int port, java.lang.String username, java.lang.String password) throws SshException
hostname
- the hostname of SSH serverport
- the port of SSH serverusername
- the SSH usernamepassword
- the SSH password
SshException
- if an SSH related exception occurspublic SshSession(java.lang.String hostname, java.lang.String username, java.lang.String password) throws SshException
hostname
- the hostname of SSH serverusername
- the SSH usernamepassword
- the SSH password
SshException
- if an SSH related exception occursMethod Detail |
---|
public void connect(int timeout) throws SshException
timeout
- timeout in milliseconds for establishing a connection
SshException
- if an SSH related exception occurspublic Ssh getSsh()
Ssh
instance.
Ssh
instancepublic void setSsh(Ssh ssh)
Ssh
instance.
ssh
- public void connect() throws SshException
SshException
- if an SSH related exception occurspublic boolean getEcho()
true
if server echo is enabled;
false
otherwisepublic void setEcho(boolean echo)
echo
- if true
server echo will
be enabled for the next connection.public java.lang.String send(java.lang.String command, long timeout) throws SshException
command
- the command to sendtimeout
- the timeout to use when waiting for shell prompt
SshException
- if regular expression prompts are used and regular expression is invalidsetShellPrompt(java.lang.String)
,
setCommandTerminator(java.lang.String)
,
SshTaskTimeoutException
public java.lang.String send(java.lang.String command) throws SshException
command
- the command to send
SshException
- if regular expression prompts are used and regular expression is invalidsetShellPrompt(java.lang.String)
,
setCommandTerminator(java.lang.String)
,
SshTaskTimeoutException
public java.lang.String send(java.lang.String command, java.lang.String prompt) throws SshException
sendWait(String, String)
method
command
- the command to sendprompt
- the prompt to wait for
SshException
- if regular expression prompts are used and regular expression is invalidsetCommandTerminator(java.lang.String)
,
sendWait(java.lang.String, java.lang.String, long)
,
SshTaskTimeoutException
public void sendNoWait(java.lang.String command) throws java.io.IOException
command
- the command to send
java.io.IOException
- if an I/O related error occurssetCommandTerminator(java.lang.String)
public java.lang.String sendWait(java.lang.String command, java.lang.String prompt, long timeout) throws SshException
command
- the command to sendprompt
- the prompt to wait fortimeout
- the timeout to use when waiting for task end prompt
SshException
- if regular expression prompts are used and regular expression is invalidsetCommandTerminator(java.lang.String)
,
SshException
,
SshTaskTimeoutException
public java.lang.String sendWait(java.lang.String command, java.lang.String prompt, boolean regex, long timeout) throws SshException
command
- the command to sendprompt
- the prompt to wait fortimeout
- the timeout to use when waiting for task end promptregex
- true if the prompt to match is a regular expression, false otherwise
SshException
- if regular expression prompts are used and regular expression is invalidsetCommandTerminator(java.lang.String)
,
setRegexMatchLength(int)
,
SshTaskTimeoutException
,
SshException
public void interrupt()
SshSession
from waiting for a response. You should only use this method in the event you wish to interrupt waiting for a response.
public java.lang.String sendWait(java.lang.String command, java.lang.String prompt) throws SshException
command
- the command to sendprompt
- the prompt to wait for
SshException
- if regular expression prompts are used and regular expression is invalidsetCommandTerminator(java.lang.String)
,
SshTaskTimeoutException
,
SshException
public java.lang.String sendWait(java.lang.String command, java.lang.String prompt, boolean regex) throws SshException
command
- the command to sendprompt
- the prompt to wait forregex
- true if prompt to match is a regular expression, false otherwise
SshException
- if regular expression prompts are used and regular expression is invalidsetCommandTerminator(java.lang.String)
,
setRegexMatchLength(int)
,
SshTaskTimeoutException
,
SshException
public void sendRaw(java.lang.String data) throws java.io.IOException
data
- the data to send
java.io.IOException
- if an I/O related error occurssetCommandTerminator(java.lang.String)
public void disconnect()
public java.lang.String getShellPrompt()
public void setShellPrompt(java.lang.String string)
string
- the shell promptpublic void setShellPrompt(java.lang.String string, boolean regex)
string
- the shell promptregex
- true if shell prompt to match is a regular expression, false otherwisesetRegexMatchLength(int)
public boolean getDebug()
public void setDebug(boolean b)
b
- true to enable debugging, false to disablesetDebugStream(java.io.PrintStream)
public void setDebugStream(java.io.PrintStream stream)
stream
- a PrintStreampublic java.io.PrintStream getDebugStream()
setDebug(boolean)
,
setDebugStream(java.io.PrintStream)
public java.lang.String getCommandTerminator()
public void setCommandTerminator(java.lang.String string)
string
- the command terminatorpublic void debug(java.lang.String msg)
msg
- public void pause(long timeout)
timeout
- time in millisecondspublic void addSshListener(SshListener listener)
listener
- a SshListenerSshListener
public void removeSshListener(SshListener listener)
listener
- a SshListenerSshListener
public void setPollingInterval(int pollingInterval)
pollingInterval
- the polling interval in milliseconds.send(java.lang.String, long)
,
sendWait(java.lang.String, java.lang.String, long)
public int getPollingInterval()
send(java.lang.String, long)
,
sendWait(java.lang.String, java.lang.String, long)
public void setRegexMatchLength(int regexMatchLength)
regexMatchLength
- public int getRegexMatchLength()
public void setCharacterSet(java.lang.String characterSet)
characterSet
- the character setpublic java.lang.String getCharacterSet()
|
Secure iNet Factory | |||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |