java - 从java在远程机器上运行linux命令

标签 java linux bash ssh remote-access

我在 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/

相关文章:

java - 找不到使用 jOOQ 在 CommonTableExpression 中选择数据的正确方法

c - 将命令 gcc 与 execlp 一起使用

linux - Linux 帧缓冲区驱动程序能否获得有关其内容的所有更改的通知?

bash - 如何在单行中输出用引号括起来的文件名?

更新后,MySQL 服务器将无法使用 'mysqld' 从终端启动

git pre-status 或 post-status Hook

java - Tomcat 的系统属性

java - 单个非标识字段中的 Hibernate UUID 生成器

java - session 与 ssl session

linux - 是否有原始套接字的安全接口(interface)?