upog
upog

Reputation: 5516

Invoking shell script contains SSH from java

I am trying to invoke a shell script containing SSH command from a java program. However it is failing with a error code 1.

My java code is as:

public class CallScript {   
    private static String filePath="";
    private static String args1="";

    public static void main(String[] args) throws Exception{
        if(args!=null && args.length > 0){      
        filePath = args[0];
        if(args.length > 1){
            args1=args[1];
        }
        }else{
            throw new Exception("File Path should be first Argument");
        }
        System.out.println(args.length);
        invokeScript(filePath,args1);
    }


    private static void invokeScript(String filePath, String arg1) throws Exception{
        System.out.println("Inside invoke Script " + arg1);
         Process p = Runtime.getRuntime().exec(filePath);
          p.waitFor();
          int exitVal = p.exitValue();
          System.out.println("The Exit Value " + exitVal);  
    }
}

I compiled the program and placed the executable jar in my Unix environment.

shell script which is invoked from java

ssh -l >test.log

I used the following command to run my java program :

java -jar invokeScript.jar /tmp/upog/test.sh

output

Inside invoke Script 
The Exit Value 1.

If I have some other command in the shell script like ls -al > test.log, the code is working with success and I am getting the return value 0.

Also if I invoke the shell script containing ssh command directly in Unix box, it is working fine.(the box have password-less connectivity)

But it is failing, if i call from java...

Any advice....

Upvotes: 4

Views: 988

Answers (2)

Konstantin Ivanov
Konstantin Ivanov

Reputation: 410

there are two possible return values: return value of 0 means success return value of >=1 means an error. One should be seeking for an error code in this case. that is why the error codes are different. There are two general approaches to use 1 as a flag to show you get an error and the error code itself is in the other place. The other way is to return 0 as success and in case of error is to return an error code itself which is >0. In nix-like systems major command return 0 or 1.

ls -al > test.log

there are two command here, the both are executed with success 0 + 0 - return value 0.

ssh -l >test.log

Here, the first command "ssh -l" executed with an error because you have not provided any arguments. No matter how ">test.log" will go you got 1 already.

your app works ok it seems - it return an exit value.

in case you need more info about the error of nested app. you may execute ssh -l >test.log 2>&1 here test.log will have errors.

or you may use something like

BufferedReader err = new BufferedReader( new InputStreamReader(p.getErrorStream()) ); // in your java app to handle error there. Note: java code might be incorrect since I use it rarely

Upd: edited command syntax wrong 2.&1 -> correct 2>&1

Upvotes: 1

Michael Kropat
Michael Kropat

Reputation: 15227

invokeScript.jar works fine for me here. The issue looks to be with /tmp/upog/test.sh. If I try to run the command I get:

$ ssh -l >test.log
ssh: option requires an argument -- l
[...]

Did you mean ssh -l someuser example.com >test.log?

When I test it with an error-free shell script, running ssh works:

$ cat >/tmp/upog/test.sh
#!/bin/bash
ssh example.com ls /tmp/upog >test.log
$ chmod +x /tmp/upog/test.sh
$ java -jar invokeScript.jar /tmp/upog/test.sh 
1
Inside invoke Script 
The Exit Value 0
$ cat test.log
bar
baz
foo

(Using example.com as the replacement text for my actual server)

Upvotes: 5

Related Questions