简体   繁体   English

从Java在远程Linux机器上运行进程(命令)

[英]run process (command) on a remote linux machine from java

I want to run a command in another server from my java code. 我想从我的Java代码在另一台服务器上运行命令。

Ex. 防爆。 My project runs in under the server ip like ---- xx.xxx.xxx.xx 我的项目在服务器IP下运行,例如---- xx.xxx.xxx.xx

But I have to run command in another server which is like ---- .yyy.yy.yyy.yy (I am having all the credentials and access for this ip) 但是我必须在另一台服务器上运行命令,例如---- .yyy.yy.yyy.yy(我拥有此ip的所有凭据和访问权限)

Is it possible? 可能吗? Any help would be appreciated. 任何帮助,将不胜感激。

You could do it multiple ways, one way to create your own server client running on both instance and communicate it over some secure protocol to instruct remotely running agent to execute command 您可以通过多种方式完成操作,一种方法是在两个实例上创建自己的服务器客户端,然后通过某种安全协议对其进行通信,以指示远程运行的代理执行命令

and if you don't want to re-invent wheel you can use SSH as underlying communication protocol and using sshj you can connect and execute command on remote machine 如果您不想重新发明轮子,则可以使用SSH作为基础通信协议,而使用sshj则可以在远程计算机上连接并执行命令


Yes you can, one of the way: 是的,您可以采用以下方法之一:

  • On your target server(yyy.yyy.yyy.yyy), run a process that listens for commands from your client machine(xxx.xxx.xxx.xxx) . 在目标服务器(yyy.yyy.yyy.yyy)上,运行一个进程,以侦听来自客户端计算机(xxx.xxx.xxx.xxx)的命令。 There are different ways to communicate between two remote jvms, you may chose any of them for example socket communication. 在两个远程jvm之间进行通信的方式有多种,您可以选择其中任意一种,例如套接字通信。

  • On target machine JVM, you can use ProcessBuilder to run the command received from client machine. 在目标计算机JVM上,可以使用ProcessBuilder来运行从客户端计算机接收到的命令。

Or just some search for frameworks already available to such a task. 或者只是搜索已经可以用于此任务的框架。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM