Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Invoking unix shell commands from java.

 
Ashish Gupta
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ashish Gupta
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Ashish Gupta
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Koushik Paul
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have the same motto, but instead of one shell command , I will need to fire a couple of them
Like
cd /root/usr/koushik
grep -in "exception" logfile >> temp.txt

then fetch the file to my application.

rm temp.txt

close the connection
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic