fixed ssh execution log

This commit is contained in:
Anthony Xu 2014-03-24 15:18:50 -07:00
parent c6d2549939
commit 2d5a58d66c

View File

@ -110,17 +110,28 @@ public class SSHCmdHelper {
InputStream stderr = sshSession.getStderr();
byte[] buffer = new byte[8192];
StringBuffer sbResult = new StringBuffer();
int currentReadBytes = 0;
while (true) {
if (stdout == null || stderr == null) {
throw new SshException("stdout or stderr of ssh session is null");
}
if ((stdout.available() == 0) && (stderr.available() == 0)) {
int conditions = sshSession.waitForCondition(ChannelCondition.STDOUT_DATA | ChannelCondition.STDERR_DATA | ChannelCondition.EOF, 120000);
int conditions = sshSession.waitForCondition(ChannelCondition.STDOUT_DATA
| ChannelCondition.STDERR_DATA | ChannelCondition.EOF | ChannelCondition.EXIT_STATUS,
120000);
if ((conditions & ChannelCondition.TIMEOUT) != 0) {
s_logger.info("Timeout while waiting for data from peer.");
break;
String msg = "Timed out in waiting SSH execution result";
s_logger.error(msg);
throw new Exception(msg);
}
if ((conditions & ChannelCondition.EXIT_STATUS) != 0) {
if ((conditions & (ChannelCondition.STDOUT_DATA | ChannelCondition.STDERR_DATA)) == 0) {
break;
}
}
if ((conditions & ChannelCondition.EOF) != 0) {
@ -131,19 +142,21 @@ public class SSHCmdHelper {
}
while (stdout.available() > 0) {
stdout.read(buffer);
currentReadBytes = stdout.read(buffer);
sbResult.append(new String(buffer, 0, currentReadBytes));
}
while (stderr.available() > 0) {
stderr.read(buffer);
currentReadBytes = stderr.read(buffer);
sbResult.append(new String(buffer, 0, currentReadBytes));
}
}
if (buffer[0] != 0)
s_logger.debug(cmd + " output:" + new String(buffer));
String result = sbResult.toString();
if (result != null && !result.isEmpty())
s_logger.debug(cmd + " output:" + result);
Thread.sleep(1000);
return sshSession.getExitStatus();
return sshSession.getExitStatus();
} catch (Exception e) {
s_logger.debug("Ssh executed failed", e);
throw new SshException("Ssh executed failed " + e.getMessage());