public interface SshSession extends SshChannel
Base interface for SSH sessions supporting all the features common to both
SSH1 and SSH2. Sessions are created through the openSessionChannel() method of
the SshClient. Once a session has been obtained
the session will not be active until you either call executeCommand(String command) or
startShell(). Once activated you can use the IO
streams to read and write to the remote process. The following code shows the
basic process:
SshConnector con = SshConnector.getInstance();
SshClient ssh = con.connect(
new SocketTransport("beagle2.sshtools.net", 22),
"martianx");
PasswordAuthentication pwd = new PasswordAuthentication();
pwd.setPassword("likeidgivethataway!");
if(ssh.authenticate(pwd)==SshAuthentication.COMPLETE) {
SshSession sesison = ssh.openSessionChannel();
if(session.requestPseudoTerminal("vt100",
80,
24,
0,
0)) {
session.startShell();
session.getOutputStream().write("ls\n".getBytes());
}
} else {
System.out.println("Authentication failed");
}
SshSession objects can't maintain state so performing a "cd c:\\temp" command would have no effect instead the Shell class should be used.
com.sshtools.ssh1.Ssh1Session
,
Ssh2Session
Modifier and Type | Field and Description |
---|---|
static int |
EXITCODE_NOT_RECEIVED
Returned from exitCode() when the remote process is still active.
|
Modifier and Type | Method and Description |
---|---|
void |
changeTerminalDimensions(int cols,
int rows,
int width,
int height)
Change the dimensions of the terminal window.
|
void |
close()
Close the session.
|
boolean |
executeCommand(String cmd)
Execute a command.
|
boolean |
executeCommand(String cmd,
String charset)
Execute a command.
|
int |
exitCode()
Return the exit code of the process once complete.
|
SshClient |
getClient()
Get the client that created this session.
|
InputStream |
getInputStream()
Get an InputStream to read the process stdout.
|
OutputStream |
getOutputStream()
Get an OutputStream to write to the process stdin.
|
InputStream |
getStderrInputStream()
Get an InputStream to read the process stderr.
|
boolean |
isClosed()
Evaluate whether the channel is closed.
|
boolean |
requestPseudoTerminal(String term,
int cols,
int rows,
int width,
int height)
The remote process may require a pseudo terminal.
|
boolean |
requestPseudoTerminal(String term,
int cols,
int rows,
int width,
int height,
byte[] modes)
The remote process may require a pseudo terminal.
|
boolean |
requestPseudoTerminal(String term,
int cols,
int rows,
int width,
int height,
PseudoTerminalModes terminalModes)
The remote process may require a pseudo terminal.
|
boolean |
startShell()
Start the users default shell.
|
addChannelEventListener, getChannelId, getMessageRouter, setAutoConsumeInput
static final int EXITCODE_NOT_RECEIVED
boolean startShell() throws SshException
true
if the shell was started, otherwise
false
SshException
SshClient getClient()
boolean executeCommand(String cmd) throws SshException
cmd
- true
if the command was accepted, otherwise
false
. This may not return false if the command is
incorrect, it should only be used as an indication that the
command was accepted and that the server will attempt to execute
it.SshException
boolean executeCommand(String cmd, String charset) throws SshException
cmd
- true
if the command was accepted, otherwise
false
. This may not return false if the command is
incorrect, it should only be used as an indication that the
command was accepted and that the server will attempt to execute
it.SshException
boolean requestPseudoTerminal(String term, int cols, int rows, int width, int height, byte[] modes) throws SshException
term
- the terminal type e.g "vt100"cols
- the number of columnsrows
- the number of rowswidth
- the width of the terminal (informational only, can be zero)height
- the height of the terminal (informational only, can be zero)modes
- an array of encoded terminal modes as described in the SSH
protocol specifications.true
if the pty was allocated, otherwise
false
SshException
boolean requestPseudoTerminal(String term, int cols, int rows, int width, int height, PseudoTerminalModes terminalModes) throws SshException
term
- the terminal type e.g "vt100"cols
- the number of columnsrows
- the number of rowswidth
- the width of the terminal (informational only, can be zero)height
- the height of the terminal (informational only, can be zero)terminalModes
- the known terminal modestrue
if the pty was allocated, otherwise
false
SshException
boolean requestPseudoTerminal(String term, int cols, int rows, int width, int height) throws SshException
term
- the terminal type e.g "vt100"cols
- the number of columnsrows
- the number of rowswidth
- the width of the terminal (informational only, can be zero)height
- the height of the terminal (informational only, can be zero)true
if the pty was allocated, otherwise
false
SshException
InputStream getInputStream() throws SshIOException
getInputStream
in interface SshIO
SshException
SshIOException
OutputStream getOutputStream() throws SshIOException
getOutputStream
in interface SshIO
SshException
SshIOException
InputStream getStderrInputStream() throws SshIOException
SshException
SshIOException
int exitCode()
void changeTerminalDimensions(int cols, int rows, int width, int height) throws SshException
cols
- rows
- width
- height
- SshException
boolean isClosed()
isClosed
in interface SshChannel
true
if the session is closed, otherwise
false
Copyright © 2014. All rights reserved.