use of org.apache.sshd.client.future.ConnectFuture in project platformlayer by platformlayer.
the class MinaSshConnectionWrapper method connect.
public void connect(TimeSpan connectTimeout) throws SshException {
if (state != SshConnectionState.NotConnected) {
throw new IllegalStateException();
}
try {
SshClient client = sshContext.client;
System.out.println("New SSH connection to " + connectionInfo.getHost());
ConnectFuture connect = client.connect(connectionInfo.getSocketAddress());
if (!connect.await(connectTimeout.getTotalMilliseconds())) {
connect.cancel();
throw new SshException("Timeout while waiting for SSH connection to " + connectionInfo.getHost());
}
this.sshClientSession = connect.getSession();
if (this.sshClientSession == null) {
throw new IllegalStateException();
}
this.state = SshConnectionState.Connected;
} catch (Exception e) {
ExceptionUtils.handleInterrupted(e);
throw new SshException("Error connecting to SSH server @" + connectionInfo.getSocketAddress(), e);
}
}
use of org.apache.sshd.client.future.ConnectFuture in project karaf by apache.
the class SshKeyFormatTest method usePemKey.
@Test
public void usePemKey() throws Exception {
SshClient client = SshClient.setUpDefaultClient();
URL testPemURL = Resources.getResource(SshKeyFormatTest.class, "test.pem");
ByteSource source = Resources.asByteSource(testPemURL);
PKCS8Key pkcs8 = new PKCS8Key(source.openStream(), null);
String sshPort = getSshPort();
client.setServerKeyVerifier(new RequiredServerKeyVerifier(pkcs8.getPublicKey()));
client.start();
ConnectFuture future = client.connect("karaf", "localhost", Integer.parseInt(sshPort));
future.await();
ClientSession session = future.getSession();
Set<ClientSessionEvent> ret = EnumSet.of(ClientSessionEvent.WAIT_AUTH);
while (ret.contains(ClientSessionEvent.WAIT_AUTH)) {
session.addPasswordIdentity("karaf");
session.auth().verify();
ret = session.waitFor(EnumSet.of(ClientSessionEvent.WAIT_AUTH, ClientSessionEvent.CLOSED, ClientSessionEvent.AUTHED), 0);
}
if (ret.contains(ClientSessionEvent.CLOSED)) {
throw new Exception("Could not open SSH channel");
}
session.close(true);
}
use of org.apache.sshd.client.future.ConnectFuture in project karaf by apache.
the class Main method connectWithRetries.
private static ClientSession connectWithRetries(SshClient client, ClientConfig config) throws Exception {
ClientSession session = null;
int retries = 0;
do {
ConnectFuture future = client.connect(config.getUser(), config.getHost(), config.getPort());
future.await();
try {
session = future.getSession();
} catch (RuntimeSshException ex) {
if (retries++ < config.getRetryAttempts()) {
Thread.sleep(config.getRetryDelay() * 1000);
System.out.println("retrying (attempt " + retries + ") ...");
} else {
throw ex;
}
}
} while (session == null);
return session;
}
use of org.apache.sshd.client.future.ConnectFuture in project karaf by apache.
the class DeployMojo method connect.
private ClientSession connect(SshClient client) throws IOException, InterruptedException {
int retries = 0;
ClientSession session = null;
do {
final ConnectFuture future = client.connect(user, host, port);
future.await();
try {
session = future.getSession();
} catch (RuntimeSshException ex) {
if (retries++ < attempts) {
Thread.sleep(TimeUnit.SECONDS.toMillis(delay));
getLog().info("retrying (attempt " + retries + ") ...");
} else {
throw ex;
}
}
} while (session == null);
return session;
}
use of org.apache.sshd.client.future.ConnectFuture in project karaf by apache.
the class SshAction method connectWithRetries.
private static ClientSession connectWithRetries(SshClient client, String username, String host, int port, int maxAttempts) throws Exception {
ClientSession session = null;
int retries = 0;
do {
ConnectFuture future = client.connect(username, host, port);
future.await();
try {
session = future.getSession();
} catch (Exception ex) {
if (retries++ < maxAttempts) {
Thread.sleep(2 * 1000);
System.out.println("retrying (attempt " + retries + ") ...");
} else {
throw ex;
}
}
} while (session == null);
return session;
}
Aggregations