【发布时间】:2013-02-15 14:30:44
【问题描述】:
我正在尝试通过 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");
【问题讨论】:
标签: java command-line ssh command