Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running Linux commands on Java through JSch

I'm establishing an SSH connection through JSch on Java and everything seemed to be working fine, until I tried to run this .sh file. The shell script's name is repoUpdate.sh and it's very simple:

echo  ' ****Repository update****'
echo  ' Location: /home/cissys/repo/'
echo -e ' Command: svn update /home/cissys/repo/2.3.0'

svn update /home/cissys/repo/2.3.0

This is the output I get directly on the linux console with the proper response of the command:

[cissys@dsatelnx5 ~]$ repoUpdate.sh
 ****Repository update****
 Location: /home/cissys/repo/
 Command: svn update /home/cissys/repo/2.3.0

At revision 9432.

Now, here's the Java code of my method with the SSH connection that tries to call this same file

public void cmremove()
{
    try
    {
        JSch jsch = new JSch();
        Session session = jsch.getSession(user, host, port);
        UserInfo ui = new SUserInfo(pass, null);
        session.setUserInfo(ui);
        session.setPassword(pass);
        session.connect();
        
        ChannelExec channelExec = (ChannelExec)session.openChannel("exec");
        
        InputStream in = channelExec.getInputStream();
        
        channelExec.setCommand("./repoUpdate.sh");
        channelExec.connect();
        
        BufferedReader reader = new BufferedReader(new InputStreamReader(in));
        String line;
        int index = 0;
        
        while ((line = reader.readLine()) != null)
        {
            System.out.println(++index + " : " + line);
        }
        
        channelExec.disconnect();
        session.disconnect();
        
        System.out.println("Done!");
    }
    catch(Exception e)
    {
        System.err.println("Error: " + e);
    }
}

And the response I get is the following:

run:
1 :  ****Repository update****
2 :  Location: /home/cissys/repo/
3 :  Command: svn update /home/cissys/repo/2.3.0
Done!
BUILD SUCCESSFUL (total time: 2 seconds)

with no output or signs of execution on the svn command (At revision 9432) whatsoever.

I'm thinking it may be closing the session at some point, not letting the command execute properly. If the updateRepo.sh`` file would've had something like cp test.txt test_2.txt`, it would do it with no problem. But i only have this problem with this and some other specific .sh files.

Any help would be appreciated.

like image 480
Miguel Ángel Avatar asked Jul 18 '12 00:07

Miguel Ángel


People also ask

How run multiple commands using JSch in Java?

Executing multiple commands at once on the remote server using Jsch. We must add a semicolon(;) between each command we want to run. So, the format will be first_command;second_command;third_command.

Can you ssh with Java?

JSch is the Java implementation of SSH2 that allows us to connect to an SSH server and use port forwarding, X11 forwarding, and file transfer. Also, it is licensed under the BSD style license and provides us with an easy way to establish an SSH connection with Java.

What is Java JSch?

JSch is a pure Java implementation of SSH2. JSch allows you to connect to an sshd server and use port forwarding, X11 forwarding, file transfer, etc., and you can integrate its functionality into your own Java programs. JSch is licensed under BSD style license.

How do I run a Java program on a remote machine?

In the Host field, enter the IP address or domain name of the host where the Java program runs. If the program runs on the same machine as the workbench, enter localhost . In the Port field, enter the port where the remote VM accepts connections. Generally, this port is specified when the remote VM is launched.


1 Answers

I suspect that your shell command is erroring out for some reason - perhaps svn isn't on your path, perhaps there are other weird environmental effects - but you're not getting the error output because you aren't looking for it. Normally, errors are sent on the stream you get from channelExec.getErrStream but in your code you only read from the getOutputStream stream.

To diagnose this, you're going to need to get those error messages. It's probably easier to get linux to use one stream for both regular output and error messages than to have your java program pulling from two streams at once, so I'd add this line as the top line of repoUpdate.sh:

exec 2>&1

That'll then cause the rest of the script to use the one stream you're reading from as both output and error.

Also, right before you call chanelExec.disconnect, in your java program you should record the exit status, and then change your "Done!" message based on what it was:

    int exitStatus = channelExec.getExitStatus();
    channelExec.disconnect();
    session.disconnect();

    if (exitStatus < 0) {
        System.out.println("Done, but exit status not set!");
    } else if (exitStatus > 0) {
        System.out.println("Done, but with error!");
    } else {
        System.out.println("Done!");
    }

If you do this, you should then get error messages that tell you why your command isn't working as you expect it should.

like image 77
Daniel Martin Avatar answered Sep 28 '22 16:09

Daniel Martin