Skip to content

Commit

Permalink
Add a new "execute" variation that returns the Channel
Browse files Browse the repository at this point in the history
This variation returns the SSH Channel, giving access to all 3 streams
and the exit status.
  • Loading branch information
Joe Hansche committed Jun 22, 2012
1 parent 46d3ff6 commit 441ebf0
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
Expand Up @@ -23,6 +23,8 @@
*/
package com.sonyericsson.hudson.plugins.gerrit.gerritevents.ssh;

import com.jcraft.jsch.ChannelExec;

import java.io.IOException;
import java.io.Reader;

Expand Down Expand Up @@ -77,6 +79,18 @@ public interface SshConnection {
*/
Reader executeCommandReader(String command) throws SshException, IOException;

/**
* Execute an ssh command on the server, without closing the session
* so that the caller can get access to all the Channel attributes from
* the server.
*
* @param command the command to execute.
* @return a Channel with access to all streams and the exit code.
* @throws IOException if it is so.
* @throws SshException if there are any ssh problems.
*/
ChannelExec executeCommandChannel(String command) throws SshException, IOException;

/**
* Disconnects the connection.
*/
Expand Down
Expand Up @@ -190,6 +190,25 @@ public synchronized Reader executeCommandReader(String command) throws SshExcept
}
}

/**
* This version takes a command to run, and then returns a wrapper instance
* that exposes all the standard state of the channel (stdin, stdout,
* stderr, exit status, etc).
*/
public synchronized ChannelExec executeCommandChannel(String command) throws SshException, IOException {
if (!isConnected()) {
throw new IllegalStateException("Not connected!");
}
try {
ChannelExec channel = (ChannelExec) connectSession.openChannel("exec");
channel.setCommand(command);
channel.connect();
return channel;
} catch (JSchException ex) {
throw new SshException(ex);
}
}

/**
* Disconnects the connection.
*/
Expand Down

0 comments on commit 441ebf0

Please sign in to comment.