如何使用Java在远程计算机上执行Linux命令? [英] How can I execute Linux commands on a remote machine using Java?

查看:1016
本文介绍了如何使用Java在远程计算机上执行Linux命令?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个安全的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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆