运行 linux 来自 java 的远程机器中的命令

Run linux command in remote machine from java

我正在 java 中处理应用程序。 我可以在我的机器主机上执行 linux 命令 (bash),但我想在像 ssh 这样的远程机器上执行这个命令。 我正在使用此代码

Process process = Runtime.getRuntime().exec(script);
    process = Runtime.getRuntime().exec(script);

    BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream()));
    String line;
    while ((line = in.readLine()) != null) {  
            System.out.println(line);
    } 

如何使用 java 代码在远程机器上执行 linux shell?

Luis:正如 Eric 所建议的,一种可能的解决方案是 运行 一个在远程服务器上执行 SSH 本身的本地脚本。

例如,如果你有一个 Linux->Linux 环境,你的脚本可能是这样的:

ssh remoteuser@remotehost 'bash -s' < localscripttoexecuteremotely.sh

在 Windows->Linux 场景中你可以这样做:

plink remoteuser@remotehost -m localscripttoexecuteremotely.sh

查看 this thread 了解更多信息。