4

I am looking for libraries that I can use to send Unix commands from my Java code. I have already tried Expect4J and Gaynmed, but could not find good documentation with them. My requirements include:

  1. Running multiple commands.
  2. Capture output for previous command.
  3. Take some action/Run different command based on output of last command.

Any pointers will be appreciated.

Lii
  • 11,553
  • 8
  • 64
  • 88
Vivek
  • 957
  • 4
  • 12
  • 18

8 Answers8

6

Looking at the names of the libraries you mentioned, it seems like you want to run the commands on a remote server using ssh.

Check out http://code.google.com/p/sshxcute/

Code is pretty easy

// Initialize a ConnBean object, parameter list is ip, username, password
ConnBean cb = new ConnBean("ip ", "username","password");
// Put the ConnBean instance as parameter for SSHExec static method getInstance(ConnBean) to retrieve a singleton SSHExec instance
ssh = SSHExec.getInstance(cb);          
// Connect to server
ssh.connect();
CustomTask sampleTask = new ExecCommand("echo 123");
ssh.exec(sampleTask);

Getting the output is easy too. Just check the link I provided.

Result res = ssh.exec(task); 
if (res.isSuccess) {
    System.out.println("Return code: " + res.rc);
    System.out.println("sysout: " + res.sysout);
} else {
    System.out.println("Return code: " + res.rc);
    System.out.println("error message: " + res.error_msg);
}
Lii
  • 11,553
  • 8
  • 64
  • 88
shadyabhi
  • 16,675
  • 26
  • 80
  • 131
  • Thanks for the answer. I have not tried sshxcute , but does it allow me to run multiple commands in one session or do I need to close session after every command. – Vivek Jan 25 '12 at 00:30
  • You can run multiple commands in one session. Check the google code page. It has all the details. The code at the bottom clearly executes two commands ct1 & ct2 in one session. – shadyabhi Jan 25 '12 at 00:33
3

The ProcessBuilder class in the Java standard library supports creating processes and reading their output. You should be able to build it to do what you need, and being in the standard library, it has pretty good documentation.

Michael Ekstrand
  • 28,379
  • 9
  • 61
  • 93
  • It's not what he is asking. He wants to run commands on a remote server using ssh. Check my answer. – shadyabhi Jan 24 '12 at 22:21
  • @Michael Thanks for the answer. I have not tried ProcessBuilder, but does it allow me to run multiple commands in one session or do I need to close session after every command. – Vivek Jan 25 '12 at 00:28
  • @user473768 `ProcessBuilder` doesn't really have "sessions", but you should be able to reuse the same `ProcessBuilder` instance for multiple processes. – Michael Ekstrand Jan 25 '12 at 01:33
2

Try this: https://github.com/fleipold/jproc

Basic usage:

String output = ProcBuilder.run("echo", "Hello World!");

assertEquals("Hello World!\n", output);
Emmanuel Osimosu
  • 5,625
  • 2
  • 38
  • 39
2

Is ProcessBuilder insufficient for your needs? You can certainly grab the output stream and get the exit value of the last command, which would be sufficient to do #3 by some regex. Not sure on #1. But doing what you want on a raw ProcessBuilder seems like it would be a pretty lightweight solution.

http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/ProcessBuilder.html

James
  • 8,512
  • 1
  • 26
  • 28
  • Thanks for the answer. I have not tried ProcessBuilder, but does it allow me to run multiple commands in one session or do I need to close session after every command. – Vivek Jan 25 '12 at 00:29
2

Have you considered Java's built-in Process class? You can retrieve the exit value of the command and get the output(s) of the shell.

Vivien Barousse
  • 20,555
  • 2
  • 63
  • 64
1

Is there something wrong with using the exec() method?

Process p = Runtime.getRuntime().exec("ls -l");

You can read from output of this command by doing the following:

BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));
tskuzzy
  • 35,812
  • 14
  • 73
  • 140
1

If your commands are simple and not interactive, you can run 'em using plain java, as below:

Process process = runtime.exec("unix command");
InputStream iStream = process.getInputStream();
InputStreamReader inputStreamReader = new InputStreamReader(iStream);
BufferedReader bufReader = new BufferedReader(inputStreamReader);
while ((String line = bufReader.readLine()) != null) {
    System.out.println("\nOUTPUT = " + line);
}

As for libraries, check ProcessBuilder.

Also, check this: How to run Unix shell script from java code?

Community
  • 1
  • 1
bchetty
  • 2,231
  • 1
  • 19
  • 26
0

Depending on what you are doing ANT is a good way to go. If you create an ant script you can then execute that script from within java. Ant would also be better supported on other environments

Scott Bonner
  • 2,890
  • 5
  • 27
  • 28