Java Code Examples for org.apache.sshd.client.session.ClientSession#addPasswordIdentity()

The following examples show how to use org.apache.sshd.client.session.ClientSession#addPasswordIdentity() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: FuseUtils.java    From keycloak with Apache License 2.0 6 votes vote down vote up
private static ClientSession openSshChannel(String username, String password) throws IOException {
    SshClient client = SshClient.setUpDefaultClient();
    client.start();
    ConnectFuture future = client.connect(username, "localhost", 8101);
    future.await();
    ClientSession session = future.getSession();

    Set<ClientSession.ClientSessionEvent> ret = EnumSet.of(ClientSession.ClientSessionEvent.WAIT_AUTH);
    while (ret.contains(ClientSession.ClientSessionEvent.WAIT_AUTH)) {
        session.addPasswordIdentity(password);
        session.auth().verify();
        ret = session.waitFor(EnumSet.of(ClientSession.ClientSessionEvent.WAIT_AUTH, ClientSession.ClientSessionEvent.CLOSED, ClientSession.ClientSessionEvent.AUTHED), 0);
    }
    if (ret.contains(ClientSession.ClientSessionEvent.CLOSED)) {
        throw new RuntimeException("Could not open SSH channel");
    }

    return session;
}
 
Example 2
Source File: ServerTest.java    From termd with Apache License 2.0 5 votes vote down vote up
private ClientSession createTestClientSession(SshServer server) throws Exception {
    ClientSession session = client.connect(getCurrentTestName(), TEST_LOCALHOST, server.getPort()).verify(7L, TimeUnit.SECONDS).getSession();
    try {
        session.addPasswordIdentity(getCurrentTestName());
        session.auth().verify(5L, TimeUnit.SECONDS);

        ClientSession returnValue = session;
        session = null; // avoid 'finally' close
        return returnValue;
    } finally {
        if (session != null) {
            session.close();
        }
    }
}
 
Example 3
Source File: PortForwardingTest.java    From termd with Apache License 2.0 5 votes vote down vote up
protected ClientSession createNativeSession() throws Exception {
    client = setupTestClient();
    PropertyResolverUtils.updateProperty(client, FactoryManager.WINDOW_SIZE, 2048);
    PropertyResolverUtils.updateProperty(client, FactoryManager.MAX_PACKET_SIZE, 256);
    client.setTcpipForwardingFilter(AcceptAllForwardingFilter.INSTANCE);
    client.start();

    ClientSession session = client.connect(getCurrentTestName(), TEST_LOCALHOST, sshPort).verify(7L, TimeUnit.SECONDS).getSession();
    session.addPasswordIdentity(getCurrentTestName());
    session.auth().verify(11L, TimeUnit.SECONDS);
    return session;
}
 
Example 4
Source File: SshFtpClient.java    From scipio-erp with Apache License 2.0 5 votes vote down vote up
@Override
public void connect(String hostname, String username, String password, Long port, Long timeout) throws IOException {
    if (port == null) port = 22L;
    if (timeout == null) timeout = 10000L;

    if (sftp != null) return;
    ClientSession session = client.connect(username, hostname, port.intValue()).verify(timeout.intValue()).getSession();
    session.addPasswordIdentity(password);
    session.auth().verify(timeout.intValue());
    // SCIPIO: 2018-09-10: changed for sshd-sftp 2.0.0
    //sftp = session.createSftpClient();
    sftp = SftpClientFactory.instance().createSftpClient(session);
}
 
Example 5
Source File: ServerTest.java    From termd with Apache License 2.0 5 votes vote down vote up
private ClientSession createTestClientSession(SshServer server) throws Exception {
    ClientSession session = client.connect(getCurrentTestName(), TEST_LOCALHOST, server.getPort()).verify(7L, TimeUnit.SECONDS).getSession();
    try {
        session.addPasswordIdentity(getCurrentTestName());
        session.auth().verify(5L, TimeUnit.SECONDS);

        ClientSession returnValue = session;
        session = null; // avoid 'finally' close
        return returnValue;
    } finally {
        if (session != null) {
            session.close();
        }
    }
}
 
Example 6
Source File: PortForwardingTest.java    From termd with Apache License 2.0 5 votes vote down vote up
protected ClientSession createNativeSession() throws Exception {
    client = setupTestClient();
    PropertyResolverUtils.updateProperty(client, FactoryManager.WINDOW_SIZE, 2048);
    PropertyResolverUtils.updateProperty(client, FactoryManager.MAX_PACKET_SIZE, 256);
    client.setTcpipForwardingFilter(AcceptAllForwardingFilter.INSTANCE);
    client.start();

    ClientSession session = client.connect(getCurrentTestName(), TEST_LOCALHOST, sshPort).verify(7L, TimeUnit.SECONDS).getSession();
    session.addPasswordIdentity(getCurrentTestName());
    session.auth().verify(11L, TimeUnit.SECONDS);
    return session;
}
 
Example 7
Source File: SSHServerTest.java    From tomee with Apache License 2.0 5 votes vote down vote up
@Test(timeout = 10000L)
public void call() throws Exception {
    final SshClient client = SshClient.setUpDefaultClient();
    client.start();
    try {
        final ClientSession session = client.connect("jonathan", "localhost", 4222).verify().getSession();
        session.addPasswordIdentity("secret");
        session.auth().verify(FactoryManager.DEFAULT_AUTH_TIMEOUT);

        final ClientChannel channel = session.createChannel("shell");
        ByteArrayOutputStream sent = new ByteArrayOutputStream();
        PipedOutputStream pipedIn = new TeePipedOutputStream(sent);
        channel.setIn(new PipedInputStream(pipedIn));
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        ByteArrayOutputStream err = new ByteArrayOutputStream();
        channel.setOut(out);
        channel.setErr(err);
        channel.open();

        pipedIn.write("properties\r\n".getBytes());
        pipedIn.flush();

        pipedIn.write("exit\r\n".getBytes());
        pipedIn.flush();

        channel.waitFor(Collections.singleton(ClientChannelEvent.CLOSED), 0);
        channel.close(false);
        client.stop();

        assertTrue(new String(sent.toByteArray()).contains("properties\r\nexit\r\n"));
        assertTrue(new String(out.toByteArray()).contains("ServerService(id=ssh)"));
    } catch (Exception e) {
        e.printStackTrace();
        fail();
    }
}
 
Example 8
Source File: AuthenticationTest.java    From termd with Apache License 2.0 4 votes vote down vote up
private static AuthFuture authPassword(ClientSession s, String user, String pswd) throws IOException {
    s.setUsername(user);
    s.addPasswordIdentity(pswd);
    return s.auth();
}
 
Example 9
Source File: AuthenticationTest.java    From termd with Apache License 2.0 4 votes vote down vote up
private static AuthFuture authPassword(ClientSession s, String user, String pswd) throws IOException {
    s.setUsername(user);
    s.addPasswordIdentity(pswd);
    return s.auth();
}