java - Expectit interact questions need to provide expect for multiple conditions -


java newbie question,

i'm attempting move of scripting perl based java , have had luck finding answers in examples here 1 has me stumped. have tried without success duplicate similar behavior perl script using expect module.

$exp->spawn("ssh -l $username $dev_ip\n"); $exp->expect(15,    [ qr/ssh -l $username $dev_ip\r/ => sub { exp_continue; } ],    [ qr/\(yes\/no\)\?\s*$/ => sub { $exp->send("yes\n"); exp_continue; } ],    [ qr/[pp]assword:\s*$/ => sub {$exp->send("$password\n");exp_continue;}],    [ qr/user/ => sub { $exp->send("$username\n"); exp_continue;} ],    [ qr/press key continue/ => sub {$exp->send("\n");exp_continue;}],    [ qr/$prompt/i => sub { $exp->send("\n");],); 

the above times out after 15 seconds if prompt line never reached. has multiple conditions see , respond while trying log in calling sub routines.

i think based on ive read need use interact similar following example, im not totally sure lambda here. provide more verbose detail?

expect.interact()     .when(contains("abc")).then(r -> system.out.println("a"))     .when(contains("xyz")).then(r -> system.err.println("b"))     .until(contains("exit"));     system.out.println("done!"); 

i suspect need example of responding prompts while staying in interact loop, , perhaps wrapping whole interact within type of timer provide default action if nothing results in log on prompt.

i have looked at

expect.expect(anyof(contains("string"), regexp("abc.*def"))); 

but didn't think meet needs because don't see way respond match while continuing others.

i think possible replicate functionality using interact in expectit. lambdas in example mentioned print text standard output stream when user types in string.

you can access 'expect' instance inside lambda function send commands or start new interactive loop.

here how approximately equivalent java code like:

        expect.withtimeout(15, timeunit.seconds)                 .interact()                 .when(regexp("\\(yes\\/no\\)\\?\\s*$"))                         .then(r -> expect.sendline("yes"))                 .when(regexp("user")).then((r) -> expect.sendline("$user"))                 .when(regexp("[pp]assword")).then((r) -> expect.sendline("$password"))                 .when(contains("press key continue")).then((r) -> expect.sendline())                 .until(regexp("$prompt")); 

note in version 0.8.1 , below have handle checked ioexception inside lambda function.


Comments

Popular posts from this blog

javascript - Thinglink image not visible until browser resize -

firebird - Error "invalid transaction handle (expecting explicit transaction start)" executing script from Delphi -

mongodb - How to keep track of users making Stripe Payments -