'How to send customized response with apache mina sshd server through exec channel?

I am using mina sshd library 2.3.0 to mock an ssh server for testing purposes.
One of our service providers only provides the ssh command through the exec channel. So the client source needs a mock server on the test case.

This mock server will receive commands through ssh exec channel and response result. Because I can't actually execute the command during the test case, I need to send a dummy response directly by java code.

I have set up the simple ssh server with the following code and successfully execute the command on the server-side. But I don't know how to send a dummy response within the SSHD framework.

Can any expert help me with this case? If I am trying the incorrect way, please feel free to tell me I am wrong.

The code:

public void startSshServer() throws IOException {
    SshServer sshd = SshServer.setUpDefaultServer();
    sshd.setPort(2222);
    sshd.setKeyPairProvider(new SimpleGeneratorHostKeyProvider(new File("d:\\hostkey.ser").toPath()));
    sshd.setPasswordAuthenticator((u, p, s) -> true);
    sshd.setPublickeyAuthenticator((s, publicKey, serverSession) -> true);
    sshd.setCommandFactory(new ProcessShellCommandFactory());
    sshd.setCommandFactory((channelSession, command) -> {
        System.out.println("command:" + command);
        Command cmd = ProcessShellCommandFactory.INSTANCE.createCommand(channelSession, command);
        return cmd;
    });
    sshd.start();
}


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source