简体   繁体   English

从Java代码运行Linux命令(远程操作系统)

[英]Run the Linux commands from java code ( remote operating system)

How do I run the ps command from Java code? 如何从Java代码运行ps命令? I know how to execute commands when the application is running locally, but I want to execute the command for a remote Linux operating system. 我知道当应用程序在本地运行时如何执行命令,但是我想为远程Linux操作系统执行命令。 For example, a Java client running on Windows. 例如,在Windows上运行的Java客户端。 The Java client needs to connect with the remote Linux system and needs to execute the ps command of Linux and get all process details. Java客户端需要与远程Linux系统连接,并且需要执行Linux的ps命令并获取所有进程详细信息。

You can run commands using Runtime.exec 您可以使用Runtime.exec运行命令

https://docs.oracle.com/javase/8/docs/api/java/lang/Runtime.html#exec-java.lang.String- https://docs.oracle.com/javase/8/docs/api/java/lang/Runtime.html#exec-java.lang.String-

Also, if you have key based authentication enabled you can run commands in remote systems using a ssh connection like this: 另外,如果启用了基于密钥的身份验证,则可以使用ssh连接在远程系统中运行命令,如下所示:

ssh user@remote.host ps -eaf

So, you can use something like: 因此,您可以使用类似:

Runtime.getRuntime().exec("ssh admin@example.com ps -eaf");    

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

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