• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Invoking unix shell commands from java.

 
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic