简体   繁体   中英

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

How do I run the ps command from Java code? 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. For example, a Java client running on Windows. 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.

You can run commands using Runtime.exec

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 user@remote.host ps -eaf

So, you can use something like:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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