use of org.apache.sshd.client.session.ClientSession.ClientSessionEvent 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.session.ClientSession.ClientSessionEvent in project karaf by apache.
the class SshCommandTestBase method openSshChannel.
private OutputStream openSshChannel(String username, String password, OutputStream... outputs) throws Exception {
client = SshClient.setUpDefaultClient();
client.start();
String sshPort = getSshPort();
Awaitility.await().ignoreExceptions().until(() -> {
ConnectFuture future = client.connect(username, "localhost", Integer.parseInt(sshPort));
future.await();
session = future.getSession();
Set<ClientSessionEvent> ret = EnumSet.of(ClientSessionEvent.WAIT_AUTH);
while (ret.contains(ClientSessionEvent.WAIT_AUTH)) {
session.addPasswordIdentity(password);
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");
}
return true;
});
channel = session.createChannel("shell");
PipedOutputStream pipe = new PipedOutputStream();
channel.setIn(new PipedInputStream(pipe));
OutputStream out;
if (outputs.length >= 1) {
out = outputs[0];
} else {
out = new ByteArrayOutputStream();
}
channel.setOut(out);
OutputStream err;
if (outputs.length >= 2) {
err = outputs[1];
} else {
err = new ByteArrayOutputStream();
}
channel.setErr(err);
channel.open();
return pipe;
}
Aggregations