我正在使用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以获得更多信息。

关于java - 从Java在远程机器上运行Linux命令,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/30306458/

10-11 23:10
查看更多