java - 使用 jsch 自动向下滚动 shell

标签 java jsch

我在 Java jsch 中遇到问题。我已经打开了一个 Shell channel ,并且有一个要从 shell 执行和读取的命令列表。

但在该命令列表中,有一个命令需要在 shell 中向下滚动才能显示直到结束。

我正在努力为该命令实现此自动滚动器。

在我的代码中我做了什么:

    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 循环来读取读取器直到其为空,并向指挥官添加一个带有“”的 printl 来尝试进一步向下滚动这个有问题的命令。

最佳答案

我设法做的是向读取缓冲区插入一个时间限制器。

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");
}

关于java - 使用 jsch 自动向下滚动 shell,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61664714/

相关文章:

java - 使用 jsch 执行多个 shell 脚本

java - 在 JSch 中获取退出状态值 -1 的原因是什么

java - org.apache.commons.vfs2.FileSystemException : Could not connect to SFTP server

带有制表符的java模式

java - 用 Java 打印到标签打印机

java - Eclipse 代码模板自动生成对字符串 setter 方法参数的 trim() 调用

java - JSCH 和多线程 java 应用程序的问题

java - 我如何在 Eclipse Juno 中使用 DDMS 访问真实 Android 设备上的 Sqlite 数据库

java - 在扑克程序中测试两对

java - JSch 0.1.53 session.connect() 抛出 "End of IO Stream Read"