aspose file tools*
The moose likes Java in General and the fly likes Invoking unix shell commands from java. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Invoking unix shell commands from java." Watch "Invoking unix shell commands from java." New topic
Author

Invoking unix shell commands from java.

Ashish Gupta
Ranch Hand

Joined: Apr 27, 2003
Posts: 61
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.

Thanks
Ashish
Ian Darwin
author
Ranch Hand

Joined: Aug 03, 2001
Posts: 64
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.

Ian


Ian Darwin
Many questions are answered in my Java Cookbook, 2nd Edition
Ashish Gupta
Ranch Hand

Joined: Apr 27, 2003
Posts: 61
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.

Please help me in solving these issues.

Thanks
Ashish
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
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.


You can redirect the output to a file by doing something like this:


Please help me in solving these issues.

Thanks
Ashish[/QB]

[ October 16, 2004: Message edited by: Layne Lund ]

Java API Documentation
The Java Tutorial
Ashish Gupta
Ranch Hand

Joined: Apr 27, 2003
Posts: 61
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.

Exec.java code
Ashish Gupta
Ranch Hand

Joined: Apr 27, 2003
Posts: 61
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.

Exec.java code


Thanks for any helpful suggestions that can be made.

Thanks
Ashish
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Invoking unix shell commands from java.
 
Similar Threads
Execute operatingsystem commands from Java classes?
JAVA on Linux
How to create tar using Java in windows os
Very much dissapointed with Linux
remove carriage return from string