开发者

Very Basic terminal in Java

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 开发者_如何学Pythonmessage 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?


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:

public static void main(String[] args) {
  final ShellSession shellSession = ...

  Thread input = new Thread() {
    @Override public void run() {
      byte[] buffer = new byte[1024];
      while(!shellSession.isClosed()) {
        int read = System.in.read(buffer);
        shellSession.getInputStream().write(buffer, read);
      }
    }
  }

  Thread output = ...
  Thread error = ...

  input.start();
  output.start();
  error.start();

}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜