java - 使用jsch自动向下滚动shell
问题描述
我在 Java jsch 中遇到问题。我已经打开了一个 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 循环以读取阅读器直到其为空,并向指挥官添加带有“”的 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");
}
推荐阅读
- spring - 如何更改 EnvironmentPostProcessor 上的 Spring Boot 活动配置文件?
- amazon-web-services - 使用 Java 查询 Amazon Aurora
- ruby - 在模块内使用类实例
- c++ - std::list insert() 使迭代器无效?
- python - 将变量定义为没有日期的开始和结束时间-Python
- stata - 在 coefplot 中仅绘制交互项
- angular - 使用 Angular HttpClient 解析服务器的 Http 请求
- encoding - 用双破折号替换 emdash
- elm - 在 elm-repl 中粘贴多行代码
- refresh - 刷新剪贴板数据?