Search code examples
javaapache-mina

SSH Apache Mina read and print file content


Hellooo i am using apache mina for the server and i want to make echoshellfactory which return some commands.

I made that but when i want command to print fr() method it prints in the eclipse console but not in the Putty console.

I know this is because response is String but is there some way to make the response to be the whole method.

public class EchoShellFactory implements Factory<Command> {

public Command create() {
    return new EchoShell();
}

public static class EchoShell implements Command, Runnable {

    private static final String SHELL_THREAD_NAME = "name";
    private static final String SHELL_PROMPT = "aaa# ====> ";
    private static final String SHELL_CMD_QUIT = "quit";
    private static final String SHELL_CMD_EXIT = "exit";
    private static final String SHELL_CMD_VERSION = "version";
    private static final String SHELL_CMD_HELP = "help";
    private static final String SHELL_CMD_FILEREADER = "fr";
    private static final String SHELL_CMD_ENTER = "";

    private InputStream in;
    private OutputStream out;
    private OutputStream err;
    private ExitCallback callback;
    private Environment environment;
    private Thread thread;
    private ConsoleReader reader;
    private PrintWriter writer;

    public InputStream getIn() {
        return in;
    }

    public OutputStream getOut() {
        return out;
    }

    public OutputStream getErr() {
        return err;
    }

    public Environment getEnvironment() {
        return environment;
    }

    public void setInputStream(InputStream in) {
        this.in = in;
    }

    public void setOutputStream(OutputStream out) {
        this.out = out;
    }

    public void setErrorStream(OutputStream err) {
        this.err = err;
    }

    public void setExitCallback(ExitCallback callback) {
        this.callback = callback;
    }

    public void start(Environment env) throws IOException {
        environment = env;
        thread = new Thread(this, SHELL_THREAD_NAME);
        thread.start();
    }

    public void destroy() {
        thread.interrupt();
    }

    public void fr() throws FileNotFoundException {
        @SuppressWarnings("resource")
        Scanner input = new Scanner(new File("C:\\Users\\john\\Desktop\\asd.txt"));

        while (input.hasNextLine()) {
            System.out.println(input.nextLine());
        }

    }

    @Override
    public void run() {
        try {

            reader = new ConsoleReader(in, new FilterOutputStream(out) {
                @Override
                public void write(final int i) throws IOException {
                    super.write(i);
                }
            });
            reader.setPrompt(SHELL_PROMPT);
            reader.addCompleter(new StringsCompleter(SHELL_CMD_QUIT, SHELL_CMD_EXIT, SHELL_CMD_VERSION,
                    SHELL_CMD_HELP, SHELL_CMD_FILEREADER));
            writer = new PrintWriter(reader.getOutput());

            // output welcome banner on ssh session startup
            writer.println("****************************************************");
            writer.println("*                Welcome to aaa.                   *");
            writer.println("****************************************************");
            writer.flush();

            String line;
            while ((line = reader.readLine()) != null) {
                handleUserInput(line.trim());
            }

        } catch (InterruptedIOException e) {
            // Ignore
        } catch (Exception e) {
            e.getLocalizedMessage();
        } finally {
            callback.onExit(0);
        }
    }

    private void handleUserInput(String line) throws InterruptedIOException, FileNotFoundException {

        if (line.equalsIgnoreCase(SHELL_CMD_QUIT) || line.equalsIgnoreCase(SHELL_CMD_EXIT))
            throw new InterruptedIOException();

        String response = null;

        if (line.equalsIgnoreCase(SHELL_CMD_VERSION))
            response = "Version 1.0.0";
        else if (line.equalsIgnoreCase(SHELL_CMD_FILEREADER))
            fr();
        else if (line.equalsIgnoreCase(SHELL_CMD_HELP))
            response = "Help is not implemented yet...";
        else if (line.equalsIgnoreCase(SHELL_CMD_ENTER))
            response = "";
        else
            response = "======> \"" + line + "\" is unknown command";

        writer.println(response);
        writer.flush();
    }
}
 }

Solution

  • You dont need the method fr().

    Instead of printing method fr(), do this:

    response = new String(Files.readAllBytes(Paths.get("C:\\Users\\John\\Desktop\\asd.txt")));