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) {
checkConnected();
ChannelExec executor = null;
try {
ByteArrayOutputStream error = null;
int j = 0;
do {
try {
executor = (ChannelExec) session.openChannel("exec");
executor.setPty(true);
} 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);
}
error = new ByteArrayOutputStream();
executor.setPty(true);
executor.setCommand(command);
ByteArrayOutputStream error = new ByteArrayOutputStream();
executor.setErrStream(error);
try {
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 errorString = error.toString();
int errorStatus = executor.getExitStatus();
@ -288,8 +306,7 @@ public class JschSshClient implements SshClient {
throw new SshException(String
.format("%s@%s:%d: Error executing command: %s", username, host, port, command), e);
}
} finally {
if (executor != null)
finally {
executor.disconnect();
}
}