0

I am trying to send commands to an SSH shell using the Apache SSHD library. I have read the JavaDoc; but it is so sparse that I am having trouble understanding how to use the library.

I'm sorry I can't link to JavaDoc, but it doesn't seem to be available on the web. I am able to set up a test that sends commands and reads results using the System.in, System.out, and System.err streams. The end goal of the project though is to send a String to the shell, process the results, send another string, process the results, etc while staying in the same shell. I know I can open up multiple ChannelExecs; but that creates multiple channels.

Essentially, I want to my program to interact with the shell.

Vance T
  • 513
  • 2
  • 5
  • 15

2 Answers2

0

https://mina.apache.org/sshd-project/tips.html

I had to modify their snippet a bit, here's what worked for me:

sshd.setCommandFactory(new CommandFactory() {
            public Command createCommand(String command) {
                return new ProcessShellFactory(" ")).create();
            }
        });
  • 1
    Although this code may answer the question, providing additional context regarding _why_ and/or _how_ it answers the question would significantly improve its long-term value. Please [edit] your answer to add some explanation. – Toby Speight May 04 '16 at 09:17
-1

If you want a fully customized and controlled shell, then you can write your own implementation for org.apache.sshd.server.Command class and inside run() method, call the java.lang.Process to execute your own shell/DOS scripts and return results to the calling program via input and output streams of Command class.

If the calling programs can execute normal shell commands like dir / mkdir / touch etc, then you can use org.apache.sshd.server.shell.InvertedShell class. There is a test class InvertedShellWrapperTest.java in the Apache SSHD package to give you a head start.

Ravi
  • 177
  • 1
  • 6