在OS X上启动终端并使用Java运行命令

时间:2015-12-29 09:52:09

标签: java macos ssh javafx exec

我正在研究JavaFX应用程序,我无法弄清楚如何在iOS上的已启动终端上编写ssh命令。

try {
    Process process = Runtime.getRuntime().exec("/usr/bin/open -a /Applications/Utilities/Terminal.app /bin/bash");
    int launched = process.waitFor();
    BufferedWriter terminal = new BufferedWriter(new OutputStreamWriter(process.getOutputStream()));
    System.out.println(launched);
    terminal.write("/usr/bin/ssh -o CheckHostIP=no -o TCPKeepAlive=yes -o StrictHostKeyChecking=no -o ServerAliveInterval=120 -o ServerAliveCountMax=100 -i ~/.aws/.ec2/dublin.pem ubuntu@"
        + selectedRow.get(publicDnsNameIndex).getValue() + "\n");
} catch (Exception e) {
    e.printStackTrace();
}

launched始终为0,因此我无法再写入该过程。

完整代码在https://github.com/gadelkareem/aws-client/blob/dev/src/main/java/com/gadelkareem/awsclient/application/Controller.java

1 个答案:

答案 0 :(得分:3)

正如this所建议的那样。我必须使用/ usr / bin / osascript

try {
    final ProcessBuilder processBuilder = new ProcessBuilder("/usr/bin/osascript",
            "-e", "tell app \"Terminal\"",
            "-e", "set currentTab to do script " +
            "(\"/usr/bin/ssh -o CheckHostIP=no -o TCPKeepAlive=yes -o StrictHostKeyChecking=no -o ServerAliveInterval=120 -o ServerAliveCountMax=100 -i ~/.aws/.ec2/dublin.pem ubuntu@" +
            selectedRow.get(publicDnsNameIndex).getValue() + "\")",
            "-e", "end tell");
    final Process process = processBuilder.start();
    process.waitFor();

} catch (Exception e) {
    e.printStackTrace();
}