use of org.apache.sshd.client.future.AuthFuture in project camel by apache.
the class SshHelper method sendExecCommand.
public static SshResult sendExecCommand(String command, SshEndpoint endpoint, SshClient client) throws Exception {
SshResult result = null;
SshConfiguration configuration = endpoint.getConfiguration();
if (configuration == null) {
throw new IllegalStateException("Configuration must be set");
}
ConnectFuture connectFuture = client.connect(null, configuration.getHost(), configuration.getPort());
// Wait getTimeout milliseconds for connect operation to complete
connectFuture.await(configuration.getTimeout());
if (!connectFuture.isDone() || !connectFuture.isConnected()) {
final String msg = "Failed to connect to " + configuration.getHost() + ":" + configuration.getPort() + " within timeout " + configuration.getTimeout() + "ms";
LOG.debug(msg);
throw new RuntimeCamelException(msg);
}
LOG.debug("Connected to {}:{}", configuration.getHost(), configuration.getPort());
ClientChannel channel = null;
ClientSession session = null;
try {
AuthFuture authResult;
session = connectFuture.getSession();
KeyPairProvider keyPairProvider;
final String certResource = configuration.getCertResource();
if (certResource != null) {
LOG.debug("Attempting to authenticate using ResourceKey '{}'...", certResource);
keyPairProvider = new ResourceHelperKeyPairProvider(new String[] { certResource }, endpoint.getCamelContext().getClassResolver());
} else {
keyPairProvider = configuration.getKeyPairProvider();
}
if (keyPairProvider != null) {
LOG.debug("Attempting to authenticate username '{}' using Key...", configuration.getUsername());
KeyPair pair = keyPairProvider.loadKey(configuration.getKeyType());
authResult = session.authPublicKey(configuration.getUsername(), pair);
} else {
LOG.debug("Attempting to authenticate username '{}' using Password...", configuration.getUsername());
authResult = session.authPassword(configuration.getUsername(), configuration.getPassword());
}
authResult.await(configuration.getTimeout());
if (!authResult.isDone() || authResult.isFailure()) {
LOG.debug("Failed to authenticate");
throw new RuntimeCamelException("Failed to authenticate username " + configuration.getUsername());
}
channel = session.createChannel(ClientChannel.CHANNEL_EXEC, command);
ByteArrayInputStream in = new ByteArrayInputStream(new byte[] { 0 });
channel.setIn(in);
ByteArrayOutputStream out = new ByteArrayOutputStream();
channel.setOut(out);
ByteArrayOutputStream err = new ByteArrayOutputStream();
channel.setErr(err);
OpenFuture openFuture = channel.open();
openFuture.await(configuration.getTimeout());
if (openFuture.isOpened()) {
channel.waitFor(ClientChannel.CLOSED, 0);
result = new SshResult(command, channel.getExitStatus(), new ByteArrayInputStream(out.toByteArray()), new ByteArrayInputStream(err.toByteArray()));
}
return result;
} finally {
if (channel != null) {
channel.close(true);
}
// need to make sure the session is closed
if (session != null) {
session.close(false);
}
}
}
Aggregations