Plain old login to ssh using /usr/bin/ssh proccess and runtime using ExpectIt

173 views
Skip to first unread message

Jay Glass

unread,
Mar 18, 2015, 6:43:57 PM3/18/15
to java-e...@googlegroups.com
Hi! Thus far - awesome library - hadnt tried the other "competitors" as I decided to try your first as you stated it did things the others couldnt, or didnt handle well! I did run into an issue, and I know it's likely a "newb" to your library thing as I am pretty familiar with plain old expect on linux - with this example, I couldnt get past the password prompt, I tried numerous things and it would just sit at the password prompt, I commented out a few things as they were earlier attempts - curious as to why it didnt work - I admit I did not spend more than half an hour to an hour on it as after searching your forum - I found уважением, Швецов Игорь post with his/her issues on 8/1/14 as well as his/hers code example which ended up working likely a charm. However I would still like to know why possibly I couldnt get past the password prompt as it would be nice to get rid of the jcraft

My code example:
try{
Process process = Runtime.getRuntime().exec(new String[]{"/usr/bin/ssh", "myName@localhost"});

Expect expect = new ExpectBuilder()
.withInputs(process.getInputStream())
.withOutput(process.getOutputStream())
.withTimeout(1, TimeUnit.SECONDS)
.withEchoOutput(System.out)
.withEchoInput(System.err)
.withExceptionOnFailure()
.build();
// try-with-resources is omitted for simplicity
//try and get past the prompt for yes/no on strict mac checking
//expect.sendLine("yes");
//expect.expect(anyOf(contains("myName@localhost's password:"), regexp(".*password:.*")));
//above didnt pwork, what about just sleep 5 and then send the pasword a couple times
Thread.sleep(5000);
expect.sendLine("myPassword");
expect.sendLine("myPassword");
expect.sendLine("myPassword");
expect.sendLine("ls -lh");
// capture the total
String total = expect.expect(regexp("^total (.*)")).group(1);
System.out.println("Size: " + total);
// capture file list
String list = expect.expect(regexp("\n$")).getBefore();
// print the result
System.out.println("List: " + list);
expect.sendLine("exit");
// expect the process to finish
expect.expect(eof());
// finally is omitted
process.waitFor();
expect.close();
}catch(Exception e){
e.printStackTrace();
}

Your thoughts? This chunk likely would have helped me to troubleshoot further but after I found the one persons example - I'm sticking with it unless I can figure out the problem with just using your example.
.withEchoInput(System.out)
.withEchoOutput(new EchoOutput() {
@Override
public void onReceive(int input, String string) throws IOException {
System.out.println("RECEIVE:" + string);
}

@Override
public void onSend(String string) throws IOException {
System.out.println("SEND:" + string);
}
})

Very much thanks and again, great library!

Sincerely

Jay

Alexey Gavrilov

unread,
Mar 19, 2015, 2:59:07 PM3/19/15
to Jay Glass, java-e...@googlegroups.com
Hi!

You are not getting the password prompt because the external ssh process reads passwords directly from TTY (/dev/tty), not from the standard input.

If you are running your code in IDE, which presumably is not bound to a TTY device, the ssh command will most probably fail. You can add the ‘-v’ option to enable debug output. On my Mac I see something like:
...
debug1: Next authentication method: keyboard-interactive
debug1: read_passphrase: can't open /dev/tty: Device not configured
debug1: Authentications that can continue: publickey,keyboard-interactive
debug1: read_passphrase: can't open /dev/tty: Device not configured
debug1: Authentications that can continue: publickey,keyboard-interactive
debug1: read_passphrase: can't open /dev/tty: Device not configured
Received disconnect from XXX: Too many authentication failures for XXX


But If you run your code inside a terminal window which connect to TTY using the Java launcher, i.e. ‘java -cp classes:expectit-core-0.6.1.jar MyClass’, you should be able to see the password prompt and even type in the password from the keyboard.
Unfortunately, the standard Java API cannot directly access TTY so the login process in this case cannot be automated.

We had a discussion with Igor (the guy with the cyrillic name you’ve mentioned) outside of the Google group on this. We came to the conclusion that if you want to run an external ssh process, you should configure the ssh password-less authentication (via public/private keys), or use ‘sshpass’ command line utility which uses direct access to TTY, or using a Java based SSH library. The latter would be my preference.

Thank you for using my library :)

Best regards,
Alexey
> --
> You received this message because you are subscribed to the Google Groups "Yet another Expect for Java" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to java-expecti...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Jay

unread,
Mar 19, 2015, 4:12:41 PM3/19/15
to java-e...@googlegroups.com, alexey1....@gmail.com
Hi Alexy!

Ahhhh! I understand what you mean, thank you for the very thorough explanation! I'm not running it in an IDE, if that makes any difference - I'm running in on a JBoss Enterprise Application server (any thoughts on a workaround with this) I could try SSH-Keys, but after testing locally, I am actually going to end up connecting to a device I believe running VX-Works which has SSH capabilities but not sure on the SSH Keys. I'll take a look at SSHPass, thanks for the tip! else, I'll continue to use a Java Based SSH Library, I take it you mean JCraft and JSch? Seeing as how it's your preference, and you have considerably way more experience - likely thats the way I'll go. I just looked at possibly doing it with telnet, anbd again - likely I'll run into the same issue with TTY. And thank you for bringing up TTY, I'd never put 2+2 together as to why some cli commands cannot be easily interacted with SSH/Telnet and thats why in the past, minus java - I always just used expect ;-)

Feel free to follow up if you want to elaborate any further on my questions or comments but if theres not much - dont worry - thank you for taking the time to reply, may bug you a bit as I go along but I'll try and keep it at a minimum.

Have a great day/night - wherever you are!

Jay

Alexey Gavrilov

unread,
Mar 19, 2015, 5:08:26 PM3/19/15
to Jay, java-e...@googlegroups.com
> Library, I take it you mean JCraft and JSch?
Correct. Personally, don’t have any preference. I used JCraft and it worked for me.

Regards,
Alexey

jay glass

unread,
Mar 19, 2015, 5:15:32 PM3/19/15
to java-e...@googlegroups.com, alexey1....@gmail.com
Cool, thanks Alexey! (Sorry I missed the extra "e" in last response)

Take it easy

Jay
Reply all
Reply to author
Forward
0 new messages