gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:52:02 25 4
gpt4 key购买 nike

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com