如何使用 Java 在远程机器上执行 Linux 命令?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7950024/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I execute Linux commands on a remote machine using Java?
提问by VamsiKrishna
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?
我有两个安全的 linux 服务器。在一台机器上,我的 Java 应用程序正在运行。我需要在 Java 中从第一台机器上的第二台机器上运行 Linux 命令。我该怎么做?
回答by Cogito
Essentially, you need to open an ssh connection to the other server from within your Java application. The OpenSSHsite has some useful information on libraries that will give you ssh support in Java.
本质上,您需要从 Java 应用程序中打开到其他服务器的 ssh 连接。在OpenSSH的网站有图书馆,这将使您在Java支持SSH一些有用的信息。
It looks like Ganymed SSH-2 for Javais the nicest of the pick there, but I haven't used any of them so you will need to look at what you need.
看起来Java 的 Ganymed SSH-2是那里最好的选择,但我没有使用它们中的任何一个,所以你需要看看你需要什么。
Once you have an ssh connection, you will be able to run commands just as if you logged in using any other ssh client.
建立 ssh 连接后,您就可以像使用任何其他 ssh 客户端登录一样运行命令。
回答by Keshav Saharia
It might be easier to check out Sockets, as you can do what you're trying to do without having to get any external libraries set up.
检查 Sockets 可能更容易,因为您可以做您想做的事情而无需设置任何外部库。
On the host machine, you want to set up a ServerSocket object, and from the client machine you open a Socket. I don't have time to type up a whole example, but check this out for a simple way to set up a server-host connection over the Internet in Java.
在主机上,您要设置一个 ServerSocket 对象,并从客户端计算机上打开一个 Socket。我没有时间输入整个示例,但请查看本文以了解一种使用 Java 在 Internet 上设置服务器-主机连接的简单方法。
http://zerioh.tripod.com/ressources/sockets.html
http://zerioh.tripod.com/ressources/sockets.html
Once you get that set up, you want to input your shell command from the ServerSocket on the computer that should execute the command, and do something around the lines of
完成设置后,您希望从应该执行命令的计算机上的 ServerSocket 输入 shell 命令,并围绕以下行做一些事情
String command = "get this from the ObjectInputStream attached to your ServerSocket";
Runtime run = Runtime.getRuntime();
Process pr = run.exec(command) ;
pr.waitFor() ;
BufferedReader buffer = new BufferedReader( new InputStreamReader( pr.getInputStream() ) ) ;
String line;
while ( ( line = buffer.readLine() ) != null )
{
System.out.println(line);
}
The tricky part is setting up a realiable host-client connection with the Sockets, but if you're doing something simple you should be fine with the example from the link above.
棘手的部分是建立与 Sockets 的真实主机-客户端连接,但如果您正在做一些简单的事情,您应该可以使用上面链接中的示例。
回答by Edwin Buck
You can do it a number of ways; however, nearly every way involves a network connection.
您可以通过多种方式做到这一点;然而,几乎所有方式都涉及网络连接。
You could write a client-server pair of Java programs, with the client connection to a server and submitting the command.
您可以编写一对客户端-服务器 Java 程序,将客户端连接到服务器并提交命令。
You could write your Java to use an existing server, like sshd, telnetd, rsh, ftpd, or a pre-existing other server which allows commands at the remote end.
您可以编写 Java 以使用现有服务器,例如 sshd、telnetd、rsh、ftpd,或允许在远程端执行命令的预先存在的其他服务器。
You could leverage an architecture which handles certain aspects of establishing a client-server pair, like RMI, SOAP, CORBA, etc.
您可以利用一种架构来处理建立客户端-服务器对的某些方面,例如 RMI、SOAP、CORBA 等。
In the end Java supports tons of networking options, so you have more ways of doing this than you think. Just make sure you don't do it in a web browser, as those JVMs are launched sandboxed, and you can't get out of the sandbox without some assistance.
最后,Java 支持大量网络选项,因此您有比您想象的更多的方法来做到这一点。只要确保不要在 Web 浏览器中执行此操作,因为这些 JVM 是在沙箱中启动的,如果没有一些帮助,您将无法脱离沙箱。
回答by talnicolas
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(此处)允许您使用 SSH 连接到远程服务器并轻松执行 shell 命令(以及许多其他内容,例如 SCP、SFTP...)。没有大量的文档资料,但你有一些真正有用的实施例子在这里(甚至你想要做什么的例子在这里)。
You can also combine Jsch with Expect4j and this way have a better control on the commands you want to execute (nice example here).
您还可以将 Jsch 与 Expect4j 结合使用,这样可以更好地控制要执行的命令(这里是一个很好的例子)。