1
votes

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.

1
It might depend on your used Mina/BouncyCastle version. Which one do you use? - SubOptimal
I'm using apache ssh core 0.5.0, slf4j-api 1.6.1 and jsch 0.1.49 - JohnnyGat
Have you tried my answer? Did it solve your issue? - SubOptimal
Actually when I included newer version of sshd-core most of my classes stop being recognizable. So I switch back to 0.5.0. As it turns out the main problem which was command handling was resolved by shellFactory - JohnnyGat

1 Answers

1
votes

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