aspose file tools*
The moose likes Java in General and the fly likes Runtime.exec() and ssh help reqd Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Runtime.exec() and ssh help reqd" Watch "Runtime.exec() and ssh help reqd" New topic
Author

Runtime.exec() and ssh help reqd

Jagat Bandhu
Greenhorn

Joined: Aug 08, 2005
Posts: 9
I have done something like the following in my code:


[code]
________________________________________________________
somefunc() returning String[]
{
String[] callargs = {"ssh", "-2", "-v", "-l", "alpha",
"122.1.18.169","ls"};


Runtime r = Runtime.getRuntime();


Process p = r.exec(nargs);


BufferedReader is = new BufferedReader(new
InputStreamReader(p.getInputSt�ream()));
BufferedReader es = new BufferedReader(new
InputStreamReader(p.getErrorSt�ream()));
OutputStream stdOut = p.getOutputStream();


ArrayList res = new ArrayList();
String line;


System.out.println("So far OK!");


while ((line = is.readLine()) != null)
{
System.out.println(line);
res.add(line);


}


String str;
while ((str = es.readLine()) != null)
{
System.out.println(str);


}


return (String[])res.toArray(new String [res.size()] );


}

________________________________________________________
[code]

The intention is to get all the file/folder listings from the remote
machine into a string array. I have already implemented a rsa key
policy, so password checking is not required.


However the problem is that after i run the code, it prints upto the
line which prints System.out.println("So far OK!"). But after that
nothing is happening. It just hangs!


Could anyone guide me to the problem?
Appreciate your help on this.


Thanks!

[ August 08, 2005: Message edited by: Jagat Bandhu ]
[ August 08, 2005: Message edited by: Jagat Bandhu ]
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

Hi,

Welcome to JavaRanch!

This is just a guess, but I'm fairly sure of it. The child process' output goes into a buffer, one for stdout, and one for stderr. It's up to the parent process to drain those buffers, or otherwise the child gets a SIGPAUSE and has to wait until there's room in the buffer again. I think that's what's happening here. The -v switch causes ssh to emit a lot of output to stderr, but your process isn't reading stderr, it's reading stdout only. The stderr buffer fills up and the child pauses before you ever see the output you expect.

The easiest thing for you to do here would be to drop the "-v" argument. Otherwise, you're going to need to use a separate thread to drain stderr.
Note that this isn't a Java thing, but a UNIX thing.


[Jess in Action][AskingGoodQuestions]
Jagat Bandhu
Greenhorn

Joined: Aug 08, 2005
Posts: 9
Thanks for your answer.
However, I'm indeed reading the error also:
Pls note the following lines in the code-
_____________________________________________
while ((str = es.readLine()) != null) {
System.out.println(str);
}
______________________________________________

This should solve any buffer problem you hae mentioned.

Also, even if I eliminate the -v option, even then the program hangs.

Any other suggestions?

Thanks!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Runtime.exec() and ssh help reqd