如何使用 Java 在远程机器上执行 Linux 命令? [英] How can I execute Linux commands on a remote machine using Java?
问题描述
我有两个安全的 linux 服务器.在一台机器上,我的 Java 应用程序正在运行.我需要在 Java 中从第一台机器的第二台机器上运行 Linux 命令.我该怎么做?
I have two secured linux servers. In one machine my Java application is running. I need to run Linux commands on second machine from first machine in Java. How might I do this?
推荐答案
Jsch(此处)允许您可以使用 SSH 连接到远程服务器并轻松执行 shell 命令(以及许多其他内容,例如 SCP、SFTP...).没有很多文档,但是您有一些非常有用的实现示例这里(和甚至你想做的一个例子 这里).
Jsch (here) allows you to connect to a remote server using SSH and executes shell commands easily (and lot of other things like SCP, SFTP...). There is not a lot of documentation, but you have a few really helpful implementation examples here (and even an example of what you want to do here).
您还可以将 Jsch 与 Expect4j 结合使用,这样可以更好地控制要执行的命令(很好的示例 此处).
You can also combine Jsch with Expect4j and this way have a better control on the commands you want to execute (nice example here).
这篇关于如何使用 Java 在远程机器上执行 Linux 命令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!