JschSshClinet bug fix for exec method

This commit is contained in:
Dmitri Babaev 2011-06-01 22:26:08 +04:00
parent ebf3527595
commit 6dc6d3581f
1 changed files with 324 additions and 307 deletions

View File

@ -261,19 +261,37 @@ public class JschSshClient implements SshClient {
public ExecResponse exec(String command) { public ExecResponse exec(String command) {
checkConnected(); checkConnected();
ChannelExec executor = null; ChannelExec executor = null;
try { ByteArrayOutputStream error = null;
int j = 0;
do {
try { try {
executor = (ChannelExec) session.openChannel("exec"); executor = (ChannelExec) session.openChannel("exec");
executor.setPty(true);
} catch (JSchException e) { } catch (JSchException e) {
// unrecoverable fail because ssh session closed
throw new SshException(String.format("%s@%s:%d: Error connecting to exec.", username, host, port), e); throw new SshException(String.format("%s@%s:%d: Error connecting to exec.", username, host, port), e);
} }
error = new ByteArrayOutputStream();
executor.setPty(true);
executor.setCommand(command); executor.setCommand(command);
ByteArrayOutputStream error = new ByteArrayOutputStream();
executor.setErrStream(error); executor.setErrStream(error);
try { try {
executor.connect(); executor.connect();
} catch (JSchException e) {
executor.disconnect();
backoffForAttempt(++j, String.format("%s@%s:%d: Failed to connect ChannelExec", username, host, port));
}
} while (j < this.sshRetries && !executor.isConnected());
if (!executor.isConnected())
throw new SshException(String.format("%s@%s:%d: Failed to connect ChannelExec executing %s",
username, host, port, command));
try {
String outputString = Strings2.toStringAndClose(executor.getInputStream()); String outputString = Strings2.toStringAndClose(executor.getInputStream());
String errorString = error.toString(); String errorString = error.toString();
int errorStatus = executor.getExitStatus(); int errorStatus = executor.getExitStatus();
@ -288,8 +306,7 @@ public class JschSshClient implements SshClient {
throw new SshException(String throw new SshException(String
.format("%s@%s:%d: Error executing command: %s", username, host, port, command), e); .format("%s@%s:%d: Error executing command: %s", username, host, port, command), e);
} }
} finally { finally {
if (executor != null)
executor.disconnect(); executor.disconnect();
} }
} }