且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何使用 Java 在远程机器上执行 Linux 命令?

更新时间:2023-12-03 23:41:10

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).