我正在尝试通过Java程序在远程主机中运行命令/可执行文件。这是我从相关主题获得的代码。我可以连接到远程主机并通过 BufferedReader in 读取流。但是,我无法使用 out.println(“ls / home / ubuntu”); 发送/执行命令。你有什么建议?我正在研究MAC OS系统。
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");