有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java使用jsch自动向下滚动shell

我在JavaJSCH中面临一个问题。我已经打开了一个Shell通道,我有一个命令列表,我想从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循环来读取读卡器直到其为null,并向commander添加一个带有“”的printl,以尝试向下滚动这个有问题的命令


共 (1) 个答案

  1. # 1 楼答案

    我设法在readbuffer中插入了一个时间限制器

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