Bookmark Topic Watch Topic
  • New Topic

Runtime exec  RSS feed

sachin y deshpande
Posts: 20
  • Mark post as helpful
  • send pies
  • Report post to moderator
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",
String arr[] = {""};
String workingFolder = "C:\\";

Runtime rt = Runtime.getRuntime();
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

String cmds[] = { "scp",
works great!

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

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 CreateProcess: "ssh root@remotelinuxbox "ls -l"" error=2

Noticably, both the 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 in advance
Ulf Dittmer
Posts: 42972
  • Mark post as helpful
  • send pies
  • Report post to moderator
Please do not post the same question to multiple forums: CarefullyChooseOneForum

Let's continue the discussion in this duplicate thread. If there was something about a reply that you didn't understand, tell us what it is.
It is sorta covered in the JavaRanch Style Guide.
    Bookmark Topic Watch Topic
  • New Topic
Boost this thread!