Автоматическая прокрутка оболочки вниз с помощью jsch - PullRequest
0 голосов
/ 07 мая 2020

У меня проблема в Java jsch. Я открыл канал оболочки, и у меня есть список команд, которые я хочу выполнить и прочитать из оболочки.

Но в этом списке команд есть одна команда, которую нужно прокрутить в оболочке, чтобы показано до конца.

Я изо всех сил пытаюсь реализовать этот автопрокрутка для этой команды.

В моем коде Что я сделал:

    ChannelShell channelShell = (ChannelShell) session.openChannel("shell");
    OutputStream inputstream_for_the_channel = channelShell.getOutputStream();
    InputStream outputstream_from_the_channel = channelShell.getInputStream();

    BufferedReader reader = new BufferedReader(new InputStreamReader(outputstream_from_the_channel));
    PrintStream commander = new PrintStream(inputstream_for_the_channel, true);

    channelShell.connect();

    if (!commands.get(0).equals("show system information | match \"System Name\"")) {
        commands.add(0, "show system information | match \"System Name\"");
    }

    String readString = "";
    String line;
    int index = 0;

    for (String cmd : commands) {
        System.out.println(cmd);

        commander.println(cmd);
        try {
            Thread.sleep(commandsSleep);
        } catch (Exception ee) {
        }

        while ((line = reader.readLine()) != null)
        {
            if (cmd.equals("admin display-config")) {
                commander.println("");
            }

            GuiApp.textAreaLog.append("\n"+ ++index + " : " + line);
            System.out.println(index + " : " + line);
            readString += (line+"\n");
        }

    }


    if (routerType.equals("nokia")) {
        commander.println("logout");
        System.out.println("logout");
        try {
            Thread.sleep(commandsSleep);
        } catch (Exception ee) {
        }
    } else if (routerType.equals("linux")){
        commander.println("exit");
    }

    commander.close();
    channelShell.disconnect();

    try {
        Thread.sleep(commandsSleep);
    } catch (Exception ee) {
    }


    return readString;

В части:

    while ((line = reader.readLine()) != null)
    {
        if (cmd.equals("admin display-config")) {
            commander.println("");
        }

        GuiApp.textAreaLog.append("\n"+ ++index + " : " + line);
        System.out.println(index + " : " + line);
        readString += (line+"\n");
    }

Это то место, где я пытался реализовать While l oop, чтобы читать считыватель до его нуля, и добавить к командующему printl с "", чтобы попытаться прокрутить эту команду c дальше вниз.

1 Ответ

0 голосов
/ 30 мая 2020

Что мне удалось сделать, так это вставить ограничитель времени в буфер чтения.

for (String cmd : commands) {
    GuiApp.textAreaLog.append("\n"+"Running Command : " + cmd);
    commander.println(cmd);
    System.out.println(cmd);
    if (cmd.equals("admin display-config")) {
        commandDelay = commandsSleep*10;
    }else{
        commandDelay = commandsSleep;
    }
    line = reader.readLine();
    while (line!=null)
    {

        System.out.println(++index + " : " + line);
        readString += (line+"\n");
        try {
            line = timeLimiter.callWithTimeout(reader::readLine, commandsSleep, TimeUnit.MILLISECONDS, true);
        } catch (Exception e) {
            e.printStackTrace();
            break;
        }
    }
    try {
        Thread.sleep(commandDelay);
    } catch (Exception e) {
        //e.printStackTrace();
    }
    GuiApp.textAreaLog.append("\n"+"Comando executado com sucesso");
}
...