开发者

Java, proper approach for controlling Linux server from within Java code?

开发者 https://www.devze.com 2023-03-14 12:46 出处:网络
I\'ve been tasked with setting up an automated system that will have to do some things on the Linux server that, from my understanding and research thus far are not able to easily be done from within

I've been tasked with setting up an automated system that will have to do some things on the Linux server that, from my understanding and research thus far are not able to easily be done from within Java, such as mounting a hard drive on the linux server. I have used some SSH over Java libraries but found them a bit difficult to use reliably. I can run the java app directly on the linux server so I'm wondering if 开发者_JAVA技巧there is a better way to make the needed calls to the server than over SSH. Any advice would be appreciated


Runtime.getRuntime().exec("some linux command");


Example:

jcomeau@intrepid:/tmp$ cat /tmp/test.java; javac test.java; java test
import java.net.*;
import java.io.*;
public class test {
 public static void main(String args[]) throws Exception {
  String line;
  Process process = Runtime.getRuntime().exec("ls");
  BufferedReader process_out = new BufferedReader(
   new InputStreamReader(process.getInputStream()));
  while ((line = process_out.readLine()) != null)
   System.out.println(line);
  process.waitFor();
 }
}
bin
hash.class
hash.java
hsperfdata_jcomeau
profile
test.class
test.java
tmpe66f4e
tmplvOd2n
tmpn8FI2Q
tmpoYaciK
tmpx27knK
vmlinux
繁體中文.txt


There are plenty of Linux webadmin tools that allow this sort of administration - Google's top hit is Webmin - written in Perl or PHP. Unless you need to integrate with existing a java code on the server I think I'd look at adapting one of these rather than starting from scratch in a language that really isn't well suited to this type of task.

Don't underestimate the potential security risks of this system - another advantage to building on the work of others.

0

精彩评论

暂无评论...
验证码 换一张
取 消