JohnnyGat
JohnnyGat

Reputation: 335

Collecting command responses from mocked SSH server (Apache MINA)

So what I'm trying to do, is create unit test that checks if invoked command (on shell via ssh connection) has a proper response. The problem is that I can't read those responses. There are not many tutorials regarding Apache MINA, so I thought maybe some of you could help me out. Here's a code

@Before
public void setUpSSHd() {

    sshd=SshServer.setUpDefaultServer();
    sshd.setPort(22999);
    sshd.setKeyPairProvider(new SimpleGeneratorHostKeyProvider("hostkey.ser"));

    sshd.setPasswordAuthenticator(new PasswordAuthenticator() {

        public boolean authenticate(String username, String password, ServerSession session) {
            // TODO Auto-generated method stub
            return true;
        }

    });

    List<NamedFactory<KeyExchange>> keyExchangeFactories;
    keyExchangeFactories = sshd.getKeyExchangeFactories();

    sshd.setKeyExchangeFactories(keyExchangeFactories);

    try {
        sshd.start();
    } catch (Exception e) {
        e.printStackTrace();
    }

}

@After
public void teardown() throws Exception { sshd.stop(); }

@Test
public void testCommands() throws Exception {

    SshClient client = SshClient.setUpDefaultClient();
    client.start();
    ClientSession session = null;
    try {

        session = client.connect("localhost", 22999).await().getSession();
        session.authPassword("none", "none").await().isSuccess();

        System.out.println("Connection established");

        final ClientChannel channel = session.createChannel(ClientChannel.CHANNEL_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("dir\r\n".getBytes());
        pipedIn.flush();

        channel.waitFor(ClientChannel.CLOSED, 0);
        channel.close(false);
        client.stop();

        System.out.println(out.toString());

    } catch (Exception e) {

        e.printStackTrace();
        fail("Cannot establish a connection");

    } finally {

        if (session != null)
            session.close(true);

    }

}

For now I simply try to print out collected response. However I get empty string everytime I try to do that. I assume there might be a problem with ssh server configuration (what shell is it supposed to use?). The best scenario would be if I could define my own commands and responses on server side and then, only check it on client side

EDIT: I've tried to manually connect to this mocked ssh server but I've got Unable to negotiate with ::1 port 22999: no matching key exchange method found. Their offer: diffie-hellman-group1-sha1 error message.

Upvotes: 1

Views: 1562

Answers (1)

SubOptimal
SubOptimal

Reputation: 22973

I would suggest you to update Apache SSH. Based the source repository the version 0.5.0 is 7 years old.

using your posted code with the default JCE provider and Apache SSH

<dependency>
    <groupId>org.apache.sshd</groupId>
    <artifactId>sshd-core</artifactId>
    <version>0.5.0</version>
<dependency>

the connect with a ssh client fails with

Their offer: diffie-hellman-group1-sha1

using a more recent Apache SSH release

<dependency>
    <groupId>org.apache.sshd</groupId>
    <artifactId>sshd-core</artifactId>
    <version>1.4.0</version>
<dependency>

the connect is successful

Upvotes: 1

Related Questions