Invoking unix shell commands from java.

Hi All, I looked around a lot in this forum and on the web but could not exactly get an answer I was looking for. Maybe I did not searched much or in the right direction.

Here is what I want to achieve. From my java code (running on windows pc) I want to 1. connect to a linux box 2. invoke a command, say 'ls -l' to list all the files and directories 3. pass back the result (may be as an array, or a stream) back to my java program.

Could you please help me with this. Code snippets, especially for connecting to the linux box, will be very helpful.

The trick here is that you need some "protocol" that will allow you both to *connect" and to *authenticate" to the UNIX system.

The most widely used implementation of such protocols used to be rsh and telnet, but these are generally regarded now as hopelessly insecure. The cookbook has examples of connecting to a Telnet server.

These protocols have largely been replaced by the SSH protocol (secure shell), see OpenSSH for a free, open-source implementation of ssh... in the C language.

For a Java implemenation, you want JSch. Download the code; it comes with examples.

Hi Ian, Thanks for the pointer to JSch. It was quite useful. I downloaded and tried the Exec.java example. In absence of any doc for JSch api, I am still strugling with 2 questions. Hopefully you or some one on this forum might have an answer.

1. When I execute the Exec.java, at session.connect(); I am prompted with a warning 'authenticity of host <host name> could not be established. RSA fingerprint .....'. Why is this warning coming? How can this be corrected. 2. Even after the unix command is executed, java program is still running. Could not figure out why this happens. 3. Output of the exec command is printed on standard output at the end. I want to capture this output in a text file or something similar.

Hi Ian, Thanks for the pointer to JSch. It was quite useful. I downloaded and tried the Exec.java example. In absence of any doc for JSch api, I am still strugling with 2 questions. Hopefully you or some one on this forum might have an answer.

1. When I execute the Exec.java, at session.connect(); I am prompted with a warning 'authenticity of host <host name> could not be established. RSA fingerprint .....'. Why is this warning coming? How can this be corrected.

From what I know, this doesn't have anything to do with Java but is a response from the SSH protocol when a host is not recognized. Every SSH tool I use issues a similar warning when I connect to a host for the first time. So the only way to "correct" this error message is clicking yes. The program should write some kind of config file that identifies hosts that are safe to use.

2. Even after the unix command is executed, java program is still running. Could not figure out why this happens.

I'm not sure about this. I would need to see the code in Exec.java.

3. Output of the exec command is printed on standard output at the end. I want to capture this output in a text file or something similar.

From what I know, this doesn't have anything to do with Java but is a response from the SSH protocol when a host is not recognized. Every SSH tool I use issues a similar warning when I connect to a host for the first time. So the only way to "correct" this error message is clicking yes. The program should write some kind of config file that identifies hosts that are safe to use.

I tried using the windows command prompt (using telnet) and got connected to linux host without any warning/error. I would hope to connect from java code as well without being prompted.

Exec.java code

Ashish Gupta
Ranch Hand

Joined: Apr 27, 2003
Posts: 61

posted Oct 18, 2004 04:53:00

0

Hi Layne

From what I know, this doesn't have anything to do with Java but is a response from the SSH protocol when a host is not recognized. Every SSH tool I use issues a similar warning when I connect to a host for the first time. So the only way to "correct" this error message is clicking yes. The program should write some kind of config file that identifies hosts that are safe to use.

I tried using the windows command prompt (using telnet) and got connected to linux host without any warning/error. I would hope to connect from java code as well without being prompted.