3

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!

nbren12
  • 634
  • 1
  • 7
  • 11
  • is the only answer I am likely to hear, "use public key auth"? – nbren12 May 10 '12 at 03:56
  • Does `/usr/libexec/ssh-askpass` exist? it seems there's an attempt to run it in order to unlock your SSH keys. – WhyNotHugo Aug 22 '12 at 06:50
  • 1
    Possible duplicate of [How to run Unix shell script in a java code using ExpectJ tool?](http://stackoverflow.com/questions/5702178/how-to-run-unix-shell-script-in-a-java-code-using-expectj-tool) – Paul Sweatte Mar 03 '16 at 16:21

0 Answers0