Java 中非常基本的终端
我需要从 Java 应用程序(在 Windows 和 Linux 上运行)向 SSH 服务器执行 5 - 10 个命令,并在 JFrame 中显示这些命令的输出。输出可能是数千行。 我选择 Ganymed-SSH 进行 SSH 通信,并执行了 startShell() 方法,该方法为我提供了登录消息和上次登录信息,但当我尝试写入 OutputStream 时,它不起作用。
以下行输出最后一次登录和操作系统信息:
while((line = outputReader.readLine()) != null){
System.out.println(line);
}
但是,以下代码似乎没有按预期工作:
OutputStream inputToShell = (shellSession.getStdin());
inputToShell.write(b);
我需要实现终端逻辑吗?如果是这样,我只需要执行一些命令,然后向用户显示输出,如何继续?
I need to execute 5 - 10 commands from a Java application (that runs on windows and linux) to an SSH server and show the output of these commands in a JFrame. The output could be thousands of lines.
I chose Ganymed-SSH for SSH communication and executed the startShell() method which gave me the login message and last login info but when I try to write to the OutputStream it's not working.
The following line outputs the last login and OS info:
while((line = outputReader.readLine()) != null){
System.out.println(line);
}
But, the following code does not seems to work as expected:
OutputStream inputToShell = (shellSession.getStdin());
inputToShell.write(b);
Do I need to implement terminal logic? If so, I just need to execute some commands and then show the output to the user, how to proceed?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我的建议是围绕 Ganymed-SSH 库创建一个简单的 Java 包装器,该包装器接受 Java 的 stdin 并将其输出到 Shell 的 stdin,并将 Shell 的 stdout 和 stderr 通过管道传输到 Java 的等效项。通过这种方式,您可以测试如何使用该库以及以何种顺序发送哪些命令等。
例如:
My advice would be to create a simple Java wrapper around the Ganymed-SSH library that takes the Java's stdin and outputs it to the Shell's stdin, and also pipes the Shell's stdout and stderr to Java's equivalents. In this way you can test how to use the library and what commands to send in which order etc.
For example: