I am trying to run commands/executables in a remote host through a Java program. Here is the code I got from a related thread. I CAN connect to the remote host and read the stream through the BufferedReader in. However, I am NOT able to send/execute commands using out.println("ls /home/ubuntu");. Do you have any recommendations? I am working on a MAC OS system.
Process p = Runtime.exec("ssh myhost");
PrintStream out = new PrintStream(p.getOutputStream());
BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream());
Thread.sleep(3000);
while (in.ready()) {
String s = in.readLine();
System.out.println(s);
}
out.println("ls /home/ubuntu");
Thread.sleep(3000);
while (in.ready()) {
String s = in.readLine();
System.out.println(s);
}
out.println("exit");