public class SshSession
extends java.lang.Object
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 and Description |
---|
SshSession(SshParameters sshParams)
Creates a new SshSession instance.
|
SshSession(SshParameters sshParams,
Ssh ssh)
Creates a new SshSession instance.
|
SshSession(Ssh ssh,
java.lang.String promptPattern,
boolean promptPatternIsRegex,
int regexMatchLength,
java.lang.String endOfLine,
java.lang.String characterSet,
int pollingInterval) |
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.
|
Modifier and Type | Method and Description |
---|---|
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 |
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 |
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 value)
Sets the session character set
|
void |
setCommandTerminator(java.lang.String value)
Sets terminator used when sending data to SSH server.
|
void |
setDebug(boolean value)
Enables or disables debugging information.
|
void |
setDebugStream(java.io.PrintStream stream)
Sets PrintStream used in outputting debug information.
|
void |
setEcho(boolean value)
Sets server echo flag for the next connection.
|
void |
setPollingInterval(int value)
Sets the polling interval used when checking for completion of commands sent using send or sendWait methods.
|
void |
setRegexMatchLength(int value)
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 value,
boolean isRegex)
Sets shell prompt to wait for before sending commands.
|
void |
setSsh(Ssh value)
Sets the underlying
Ssh instance. |
public SshSession(Ssh ssh, java.lang.String promptPattern, boolean promptPatternIsRegex, int regexMatchLength, java.lang.String endOfLine, java.lang.String characterSet, int pollingInterval)
public SshSession(SshParameters sshParams, Ssh ssh)
sshParams
- - the SshParameters instance.ssh
- - the Ssh instance.SshException
- - the exception instance that is thrown if an error occurs.public SshSession(SshParameters sshParams)
sshParams
- an SshParameters instanceSshException
- if an SSH related exception occurspublic SshSession(java.lang.String hostname, int port, java.lang.String username, java.lang.String password)
hostname
- the hostname of SSH serverport
- the port of SSH serverusername
- the SSH usernamepassword
- the SSH passwordSshException
- if an SSH related exception occurspublic SshSession(java.lang.String hostname, java.lang.String username, java.lang.String password)
hostname
- the hostname of SSH serverusername
- the SSH usernamepassword
- the SSH passwordSshException
- if an SSH related exception occurspublic Ssh getSsh()
Ssh
instance.Ssh
instancepublic void setSsh(Ssh value)
Ssh
instance.value
- public java.lang.String getShellPrompt()
public void setShellPrompt(java.lang.String string)
string
- the shell promptpublic void setShellPrompt(java.lang.String value, boolean isRegex)
value
- the shell promptisRegex
- true if shell prompt to match is a regular expression, false otherwisesetRegexMatchLength(int)
public java.lang.String getCommandTerminator()
public void setCommandTerminator(java.lang.String value)
value
- the command terminatorpublic boolean getEcho()
true
if server echo is enabled;
false
otherwisepublic void setEcho(boolean value)
value
- if true
server echo will
be enabled for the next connection.public int getRegexMatchLength()
public void setRegexMatchLength(int value)
value
- public java.lang.String getCharacterSet()
public void setCharacterSet(java.lang.String value)
value
- the character setpublic int getPollingInterval()
send(java.lang.String, java.lang.String)
,
sendWait(java.lang.String, java.lang.String, boolean, long)
public void setPollingInterval(int value)
value
- the polling interval in milliseconds.send(java.lang.String, java.lang.String)
,
sendWait(java.lang.String, java.lang.String, boolean, long)
public boolean getDebug()
public void setDebug(boolean value)
value
- 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 void addSshListener(SshListener listener)
listener
- a SshListenerSshListener
public void removeSshListener(SshListener listener)
listener
- a SshListenerSshListener
public void connect(int timeout) throws SshException
timeout
- timeout in milliseconds for establishing a connectionSshException
- if an SSH related exception occurspublic void connect() throws SshException
SshException
- if an SSH related exception occurspublic void disconnect()
public void sendRaw(java.lang.String data) throws java.io.IOException
data
- the data to sendjava.io.IOException
- if an I/O related error occurssetCommandTerminator(java.lang.String)
public void sendNoWait(java.lang.String command) throws java.io.IOException
command
- the command to sendjava.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, 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 otherwiseSshException
- if regular expression prompts are used and regular expression is invalidsetCommandTerminator(java.lang.String)
,
setRegexMatchLength(int)
,
SshTaskTimeoutException
,
SshException
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 promptSshException
- 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) throws SshException
command
- the command to sendprompt
- the prompt to wait forregex
- true if prompt to match is a regular expression, false otherwiseSshException
- if regular expression prompts are used and regular expression is invalidsetCommandTerminator(java.lang.String)
,
setRegexMatchLength(int)
,
SshTaskTimeoutException
,
SshException
public java.lang.String sendWait(java.lang.String command, java.lang.String prompt) throws SshException
command
- the command to sendprompt
- the prompt to wait forSshException
- if regular expression prompts are used and regular expression is invalidsetCommandTerminator(java.lang.String)
,
SshTaskTimeoutException
,
SshException
public java.lang.String send(java.lang.String command, java.lang.String prompt) throws SshException
sendWait(String, String)
methodcommand
- the command to sendprompt
- the prompt to wait forSshException
- if regular expression prompts are used and regular expression is invalidsetCommandTerminator(java.lang.String)
,
sendWait(java.lang.String, java.lang.String, boolean, long)
,
SshTaskTimeoutException
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 promptSshException
- 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 sendSshException
- if regular expression prompts are used and regular expression is invalidsetShellPrompt(java.lang.String)
,
setCommandTerminator(java.lang.String)
,
SshTaskTimeoutException
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.Copyright © JSCAPE LLC. 1999-2021. All Rights Reserved