• Post Reply Bookmark Topic Watch Topic
  • New Topic

Runtime exec on windows  RSS feed

 
sachin y deshpande
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I am trying to execute, from my local windows box, a command (ssh) on a remote linux box using Java's Runtime exec

String cmds[] = { "ssh",
"root@remotelinuxbox",
"ls"
};
String arr[] = {""};
String workingFolder = "C:\\";

Runtime rt = Runtime.getRuntime();
rt.traceInstructions(true);
rt.traceMethodCalls(true);
Process proc = rt.exec(cmds, arr, new File(workingFolder));
System.out.println("ExecInput.ssh()" + " ssh'ed");

// get its output (your input) stream
InputStream inputstream = proc.getInputStream();
InputStreamReader inputstreamreader = new InputStreamReader(inputstream);
BufferedReader bufferedreader = new BufferedReader(inputstreamreader);

InputStream inputstreamerr = proc.getErrorStream();
InputStreamReader isr = new InputStreamReader(inputstreamerr);
BufferedReader br = new BufferedReader(isr);

String lineErr;
while ((lineErr = br.readLine()) != null) {
System.out.println("lineErr-" + lineErr);
}

The output of the above code results in :

ExecInput.ssh() ssh'ed
lineErr-socket: Operation not permitted
lineErr-ssh: connect to host remotelinuxbox port 22: Operation not permitted
Exit value :255


Funnily,
String cmds[] = { "scp",
"ST_list.xml",
"root@remotelinuxbox:ST_list.xml"};
works great!

Can someone tell me what do I need to correct here?

Note:
1) Have tried other combinations (am skeptical about these though)

a) String cmds[] = { "ssh root@remotelinuxbox ls"};
b) String cmds[] = { "ssh",
"root@remotelinuxbox 'ls -l'"};
c) String cmds[] = { "ssh",
"root@remotelinuxbox | 'ls -l'"};
These combinations yield
IO Stack :java.io.IOException: CreateProcess: "ssh root@remotelinuxbox "ls -l"" error=2


Help me out of this please.

Thanks in advance
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From the command line, what happens when you just type "ssh root@remotelinuxbox" ? Can you log in that way?
 
sachin y deshpande
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Both the below commands when executed on the command prompt work perfectly as expected! The login happens and they execute as well.
1) scp ST_list.xml root@remotelinuxbox:ST_list.xml
2) ssh root@remotelinuxbox ls

Thanks.
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not sure if that is the problem, but the code does not seem to be reading the input stream.
 
sachin y deshpande
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have omitted the part which reads from input (not that it really matters).
Here is the rest of the part:

...
...
String lineErr;
while ((lineErr = br.readLine()) != null) {
System.out.println("lineErr-" + lineErr);
}

// read the ls output
String lineOut;
while ((lineOut = bufferedreader.readLine()) != null) {
System.out.println("lineOut-" + lineOut);
}

try {
if (proc.waitFor() != 0) {
System.err.println("Exit value :" +
proc.exitValue());
}
}
catch (InterruptedException e) {
e.printStackTrace();
System.err.println("Iterrupted Stack :" + e);
}
...
...


Thanks again
 
Martijn Verburg
author
Bartender
Posts: 3275
5
Eclipse IDE Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Sachin, please UseCodeTags in future, it makes it easier for everyone to read your questions
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Each stream should be read in a separate Thread, otherwise you get mystery lockups.

Bill
 
sachin y deshpande
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Agreed that 'Each stream should be read in a separate Thread' to avoid lockups.
But thats not whats happening currently!
It would be great if someone has an explanation, or better still, a solution.
Thanks.
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1) Have you got some kind of Windows firewall turned on (the built-in one, or one that came with an anti-virus system) which might be blocking ssh from working when Java launches it?

2) Is it possible that you've got two different ssh programs, one which works, and one that doesn't, and Java is launching the one that doesn't? Can you use the absolute path to the ssh that works?
 
sachin y deshpande
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1) I do have a firewall which came with the anti-virus. Turned it off. Tried again. Same results.
2) Verified that I dont have another ssh program. And I dont have it.

Any other suggestions?
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!