}
}
int sshExecute(String command, final OutputStream stdout, final OutputStream stderr, ProcessStartListener listener,
TimeSpan timeout) throws SshException, IOException, InterruptedException {
ChannelSession sshChannel = null;
try {
sshChannel = ensureConnected().openSession(command);
sshChannel.setIn(new ByteArrayInputStream(new byte[0]));
sshChannel.setOut(stdout);
sshChannel.setErr(stderr);
try {
sshChannel.open().await(DEFAULT_SSH_CONNECT_TIMEOUT.getTotalMilliseconds());
} catch (Exception e) {
ExceptionUtils.handleInterrupted(e);
throw new SshException("Ssh error opening channel", e);
}
if (listener != null) {
throw new UnsupportedOperationException();
// listener.startedProcess(sshChannel.getStdin());
}
if (timeout == null) {
timeout = TimeSpan.ZERO;
}
// Wait for everything to finish
int flags = sshChannel.waitFor(ClientChannel.EOF | ClientChannel.CLOSED, timeout.getTotalMilliseconds());
if ((flags & ClientChannel.TIMEOUT) != 0) {
closeAndRemoveFromPool();
throw new SshException("Timeout while waiting for SSH task to complete. Timeout was " + timeout);
}
flags = sshChannel.waitFor(ClientChannel.EXIT_STATUS, 30000);
if ((flags & ClientChannel.TIMEOUT) != 0) {
closeAndRemoveFromPool();
throw new SshException("Timeout while waiting for exit code. Timeout was " + timeout);
}
Integer exitCode = getExitStatus(sshChannel);
if (exitCode == null) {
closeAndRemoveFromPool();
throw new SshException("No exit code returned");
}
return exitCode;
} finally {
if (sshChannel != null) {
sshChannel.close(false);
}
}
}