Skip to content
Snippets Groups Projects
Commit d0c3a6cc authored by Jan Kožusznik's avatar Jan Kožusznik
Browse files

support ssh sessions

parent 36acadb1
No related branches found
No related tags found
No related merge requests found
......@@ -6,6 +6,7 @@ import com.jcraft.jsch.Identity;
import com.jcraft.jsch.JSchException;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.LinkedList;
......@@ -37,20 +38,29 @@ public class SshCommandClient extends AbstractBaseSshClient {
super(hostName, userName, keyFile, pass);
}
public List<String> executeCommand(String command) {
List<String> result = new LinkedList<>();
public SshExecutionSession openSshExecutionSession(String command) {
try {
ChannelExec channelExec = (ChannelExec) getConnectedSession().openChannel(
"exec");
InputStream in = channelExec.getInputStream();
channelExec.setCommand(command);
channelExec.connect();
return new P_SshExecutionSession(channelExec);
}
catch (Exception e) {
log.error("Error: ", e);
throw new RuntimeException(e);
}
}
public List<String> executeCommand(String command) {
List<String> result = new LinkedList<>();
try (SshExecutionSession session = openSshExecutionSession(command)) {
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
BufferedReader reader = new BufferedReader(new InputStreamReader(session
.getStdout()));
BufferedReader errReader = new BufferedReader(new InputStreamReader(
channelExec.getErrStream()));
session.getStderr()));
String line;
while ((line = reader.readLine()) != null) {
......@@ -61,8 +71,7 @@ public class SshCommandClient extends AbstractBaseSshClient {
errors.add(line);
}
int exitStatus = channelExec.getExitStatus();
channelExec.disconnect();
int exitStatus = session.getExitStatus();
if (exitStatus < 0) {
log.debug("Done, but exit status not set!");
......@@ -92,4 +101,34 @@ public class SshCommandClient extends AbstractBaseSshClient {
}
return true;
}
private class P_SshExecutionSession implements SshExecutionSession {
private ChannelExec channel;
public P_SshExecutionSession(ChannelExec channel) {
this.channel = channel;
}
@Override
public InputStream getStdout() throws IOException {
return channel.getInputStream();
}
@Override
public InputStream getStderr() throws IOException {
return channel.getErrStream();
}
@Override
public int getExitStatus() {
return channel.getExitStatus();
}
@Override
public void close() {
channel.disconnect();
}
}
}
package cz.it4i.fiji.scpclient;
import java.io.Closeable;
import java.io.IOException;
import java.io.InputStream;
public interface SshExecutionSession extends Closeable {
InputStream getStdout() throws IOException;
InputStream getStderr() throws IOException;
int getExitStatus();
@Override
void close();
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment