0

To be more specific, what i really want to do is to open two new terminals. From terminal_1 i want to ssh @host1 and run a program1 to host1. From terminal_2 i want to ssh @host2 and run a program2 to host2. I need the output of program1 to be viewed on terminal_1 and output of program2 to be viewed on terminal_2.

(I have managed to open xterm and ssh @host.I tried to pass a second command "&&java echo_1" but it does nothing at all)

Here is the code:

import java.io.*;

public class multi1 implements Runnable {

    public void run() {

    try {
        String ss = null;
            Runtime obj = null;
        String[] newcmd = new String[]{"/usr/bin/xterm","-hold","-e","ssh andreas@192.168.0.0&&java echo_1"};

        Process p = Runtime.getRuntime().exec(newcmd);

            BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));
            BufferedReader stdError = new BufferedReader(new InputStreamReader(p.getErrorStream()));

            while ((ss = stdInput.readLine()) != null) {
                    System.out.println(ss);
            }
        }catch (IOException e) {
            System.out.println("FROM CATCH" + e.toString());
        }
    }

    public static void main(String[] args) throws Exception {
        Thread th = new Thread(new multi1());
        th.start();
    //Thread th2 = new Thread(new multi1());
    //th2.start();
    }
}
Brian Agnew
  • 268,207
  • 37
  • 334
  • 440
Andreas
  • 3
  • 3

3 Answers3

0

I rather use jsch or other ssh library and do it without terminals and exec.

Aviram Segal
  • 10,962
  • 3
  • 39
  • 52
  • The echo_1 is a program that will be running for a long time and giving specific output after events arrived. I need to monitor that output locally. Is that possible with jsch library? Thanks – Andreas Jul 25 '12 at 16:02
  • With jsch you can connect with SSH and get streams to output and input,about time it should be an issue – Aviram Segal Jul 26 '12 at 06:08
  • Hey Aviram, work is done using the jsch thanks! I have a problem you might have the solution.. If i execute ls or java javaprogram or ./cprogram, in.available is >0 and i retrieve the output, but when i execute a program /usr/local/bin/click_program, in.available() is 0 but output is printed on my terminal. How is that possible?? Why i cant retrieve that output too? – Andreas Jul 26 '12 at 15:41
  • Regardless too jcsh, in.available tells you how much you can currently read from the input stream, if its 0 that doesn't mean you will not get input a little later. you should just read the input stream until it ends (as you read any other stream). btw, if the answer helped i'll appreciate if you accept it. – Aviram Segal Jul 26 '12 at 16:19
0

One concern I have immediately is that you're not handling the stdout/stderr from the spawned process. If you don't do this, it's likely that you'll block the output from the process and the process itself. See this answer for more info.

Community
  • 1
  • 1
Brian Agnew
  • 268,207
  • 37
  • 334
  • 440
0
BufferedReader stdError = .. 

..would be flagged a warning by Eclipse as it does not seem to be referenced again. That in turn would indicate that the error stream is not being consumed. Do so.

For more tips on using a Process, see the article linked in the runtime.exec info. page. Also, better to use a ProcessBuilder, which can merge the System.out and System.err, making it easier to consume both with one loop.

Community
  • 1
  • 1
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433