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,18 +110,29 @@ public class SSHCmdHelper {
InputStream stderr = sshSession.getStderr(); InputStream stderr = sshSession.getStderr();
byte[] buffer = new byte[8192]; byte[] buffer = new byte[8192];
StringBuffer sbResult = new StringBuffer();
int currentReadBytes = 0;
while (true) { while (true) {
if (stdout == null || stderr == null) { if (stdout == null || stderr == null) {
throw new SshException("stdout or stderr of ssh session is null"); throw new SshException("stdout or stderr of ssh session is null");
} }
if ((stdout.available() == 0) && (stderr.available() == 0)) { 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) { if ((conditions & ChannelCondition.TIMEOUT) != 0) {
s_logger.info("Timeout while waiting for data from peer."); 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; break;
} }
}
if ((conditions & ChannelCondition.EOF) != 0) { if ((conditions & ChannelCondition.EOF) != 0) {
if ((conditions & (ChannelCondition.STDOUT_DATA | ChannelCondition.STDERR_DATA)) == 0) { if ((conditions & (ChannelCondition.STDOUT_DATA | ChannelCondition.STDERR_DATA)) == 0) {
@ -131,18 +142,20 @@ public class SSHCmdHelper {
} }
while (stdout.available() > 0) { while (stdout.available() > 0) {
stdout.read(buffer); currentReadBytes = stdout.read(buffer);
sbResult.append(new String(buffer, 0, currentReadBytes));
} }
while (stderr.available() > 0) { while (stderr.available() > 0) {
stderr.read(buffer); currentReadBytes = stderr.read(buffer);
sbResult.append(new String(buffer, 0, currentReadBytes));
} }
} }
if (buffer[0] != 0) String result = sbResult.toString();
s_logger.debug(cmd + " output:" + new String(buffer)); if (result != null && !result.isEmpty())
s_logger.debug(cmd + " output:" + result);
Thread.sleep(1000);
return sshSession.getExitStatus(); return sshSession.getExitStatus();
} catch (Exception e) { } catch (Exception e) {
s_logger.debug("Ssh executed failed", e); s_logger.debug("Ssh executed failed", e);