How to wrap rsync with interactive login using Java?

624 views Asked by At

I am trying to wrap rsync using java, and I have been having difficulty dealing with interactive login. I can get the unix command line utility expect to work, but I cannot seem to figure out the java equivalent ExpectJ. Here is my current code:

import java.io.IOException;

import expectj.ExpectJ;
import expectj.Spawn;


public class ExpectjTest {

    /**
     * @param args
     * @throws IOException 
     */
    public static void main(String[] args) throws IOException {
        // TODO Auto-generated method stub
        String mypassword= "example";
        String cmd = "rsync  -e ssh --list-only --recursive   user@host:~/\n";
        ExpectJ expectinator = new ExpectJ();
        Spawn rsync = expectinator.spawn("/bin/bash");
        try {

            rsync.send(cmd);
            rsync.expect("password:",2);
            rsync.send(mypassword+"\n");
            rsync.expectClose();


        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }   
    }
}

When I run this, I get the error:

ssh_askpass: exec(/usr/libexec/ssh-askpass): No such file or directory
Permission denied (gssapi-keyex,gssapi-with-mic,publickey,keyboard-interactive).

It seems like the normal ssh interactive login is not being initiated because I am calling rsync from java. The analogous shell script using expect works just fine. Also, when I use public key authorization, similar java code seems to work. If I just wanted to use ssh, I would use some library like JSch, but I don't really trust any of the java rsync libraries, so I want to wrap the command line tool.

Thanks for your help!

0

There are 0 answers