Looking for a bit more detail in using module I think this needs expect.interact just not clear how to implement it! Examples needed

59 views
Skip to first unread message

Thomas Campion

unread,
Sep 14, 2016, 12:29:47 AM9/14/16
to Yet another Expect for Java
I am trying to migrate from Perl expect to Java and am having trouble grasping how to write similar logic with expectit. I'm writing automation application that logs onto a network device and I'm trying to come up with universal method for doing it. I need to be able to match multiple things and respond accordingly.

here is Perl code, Im trying to do similar in java using expectit.

                     $exp-> spawn("ssh -l $username $device_ip\n");
                     $exp->expect(10,                                                                                            <- default timeout if no matches below found
                         [ qr/ssh -l $username $device_ip\r/ => sub { exp_continue; } ],                        <- regex match echoed command, continue matching
                         [ qr/\(yes\/no\)\?\s*$/ => sub { $exp->send("yes\n"); exp_continue; } ],            <- regex match some devices ask a question before presenting login prompt answer and continue matching
                         [ qr/[Pp]assword:\s*$/ => sub { $exp->send("$password\n"); exp_continue;} ], <- regex match password prompt, send password, and continue matching
                         [ qr/User/ => sub { $exp->send("$username\n"); exp_continue;} ],              <- regex username prompt, send username , and continue matching
                         [ qr/Login invalid/ => sub { $log .= 'bad un/pw';} ],                                      <- regex bad credentials, log it and end expect matching
                         [ qr/Press any key to continue/ => sub { $exp->send("\n"); exp_continue;} ], <- regex match some devices ask a question before presenting login prompt answer and continue matching
                         [ qr/#/ => sub {  $this_login_priv_mode = 'enable'; } ],                                   <- got enable prompt good news end this expect move onto the next thing
                         [ qr/>$/ => sub {
                                $exp->send("en\n");
                                $exp->expect(10, -re, "[Pp]assword:");                                                <- regex see user mode prompt try getting to privileged mode, and continue expect matching
                                $exp->send("$password}\n");
                                exp_continue; } ],
                         [ qr/closed/  } ],
                         [ qr/Connection refused/} ],
                         [ qr/Permission denied/ } ],                                                            <- regex matching other fault conditions, discontinue matching
                         [ qr/Access denied/ } ],
                         [ timeout => } ],
                     );


The above gives me a universal expect to log into several different vendor devices ultimately getting me to an enable prompt on a device to do config changes or show other output. I would like to construct expectit code to provide similar functionality. other things I do which are not clear for example I clear the capture buffer and send carriage return and collect everything before the # this gives me the device prompt which I capture to use in other expects. I found I needed to be more accurate with the device prompt to know when a command has finished executing. If I just used a single "#" for example I would sometimes get false positive matches when # appeared in a device where i wasn't expecting it.

I think expecit probably does all I need to do just not enough examples to cobble something together. I appreciate anyone who responds with some tips on how to move this forward. and thank you Alexey for all you effort on this project.


Thomas Campion

unread,
Sep 14, 2016, 12:31:47 AM9/14/16
to Yet another Expect for Java

Alexey Gavrilov

unread,
Sep 14, 2016, 3:32:30 PM9/14/16
to Thomas Campion, Yet another Expect for Java
Thomas, thank you for your feedback.
I’m a bit busy at the moment but I’ll try to give some tips later this week.  

--
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.

Thomas Campion

unread,
Sep 14, 2016, 3:42:28 PM9/14/16
to Yet another Expect for Java, tc1...@gmail.com, Alexey1....@gmail.com
Awesome! thanks for looking at this.

tcof...@gmail.com

unread,
Nov 26, 2016, 1:38:09 AM11/26/16
to Yet another Expect for Java, tc1...@gmail.com, Alexey1....@gmail.com
On Wednesday, September 14, 2016 at 2:32:30 PM UTC-5, Alexey Gavrilov wrote:
Hi Alex,

Hope you are doing well. My schedule has gotten free for me and was hoping to take a look at this again. Please let me know if you are able to provide some examples of what I described above.

Alexey Gavrilov

unread,
Dec 3, 2016, 7:28:49 AM12/3/16
to tcof...@gmail.com, Yet another Expect for Java, tc1...@gmail.com
Hope you are doing well. My schedule has gotten free for me and was hoping to take a look at this again. Please let me know if you are able to provide some examples of what I described above. 

Still quite busy, sorry.

At this point I can only promise to release a new ExpectIt version on Christmas in which I plan to document all capabilities of Expect#interact()  method that would be used in your case. For now I have only this example:

Regards,
Alexey

Thomas Campion

unread,
Dec 3, 2016, 11:52:53 AM12/3/16
to Alexey Gavrilov, tcof...@gmail.com, Yet another Expect for Java

Alexey,

 

 

I understand and Christmas timeline is totally OK. I hope to take deeper dive into what your tool does as it is a critical piece in the application I will be writing. Looking forward to thorough documentation. If I can be of any help testing or whatever please reach out. I appreciate the level of effort you have provided. I’m hoping once I get some more java experience I can contribute to this development community as well.

 

I work at Verizon and anticipate using your tool heavily to preform network management tasks and automating our processes aligned with that activity.

 

Thanks again for your timely support.




Avast logo

This email has been checked for viruses by Avast antivirus software.
www.avast.com


Alexey Gavrilov

unread,
Dec 26, 2016, 4:05:48 AM12/26/16
to Thomas Campion, Yet another Expect for Java
Hi Thomas,

On 14 Sep 2016, at 21:42, Thomas Campion <tc1...@gmail.com> wrote:

here is Perl code, Im trying to do similar in java using expectit.

                     $exp-> spawn("ssh -l $username $device_ip\n");
                     $exp->expect(10,                                                                                            <- default timeout if no matches below found
                         [ qr/ssh -l $username $device_ip\r/ => sub { exp_continue; } ],                        <- regex match echoed command, continue matching
                         [ qr/\(yes\/no\)\?\s*$/ => sub { $exp->send("yes\n"); exp_continue; } ],            <- regex match some devices ask a question before presenting login prompt answer and continue matching 
                         [ qr/[Pp]assword:\s*$/ => sub { $exp->send("$password\n"); exp_continue;} ], <- regex match password prompt, send password, and continue matching 
                         [ qr/User/ => sub { $exp->send("$username\n"); exp_continue;} ],              <- regex username prompt, send username , and continue matching 
                         [ qr/Login invalid/ => sub { $log .= 'bad un/pw';} ],                                      <- regex bad credentials, log it and end expect matching 
                         [ qr/Press any key to continue/ => sub { $exp->send("\n"); exp_continue;} ], <- regex match some devices ask a question before presenting login prompt answer and continue matching 
                         [ qr/#/ => sub {  $this_login_priv_mode = 'enable'; } ],                                   <- got enable prompt good news end this expect move onto the next thing 
                         [ qr/>$/ => sub {
                                $exp->send("en\n");
                                $exp->expect(10, -re, "[Pp]assword:");                                                <- regex see user mode prompt try getting to privileged mode, and continue expect matching 
                                $exp->send("$password}\n");
                                exp_continue; } ],
                         [ qr/closed/  } ],
                         [ qr/Connection refused/} ],
                         [ qr/Permission denied/ } ],                                                            <- regex matching other fault conditions, discontinue matching
                         [ qr/Access denied/ } ],
                         [ timeout => } ],
                     );

I don’t have a good way to test but some like the following may work for you (make sure you bump the ExpectIt version to 0.8.2):

final Result result = expect.interact()
.when(contains("yes/no")).then((r) -> expect.sendLine("yes"))
.when(contains("Press any key to continue")).then((r) -> expect.sendLine())
.when(regexp("[Pp]assword:\\s")).then((r) -> expect.sendLine("SECRET"))
.when(regexp("User")).then((r) -> expect.sendLine("USERNAME"))
.when(regexp(">$")).then((r) -> expect.sendLine("en"))
.until(
anyOf(
contains("#"),
contains("closed"),
contains("Login invalid"),
contains("Connection refused"),
contains("Permission denied"),
contains("Access denied")));
if ("#".equals(result.group())) {
System.out.println("this_login_priv_mode");
} else if ("Login invalid".equals(result.group())) {
System.out.println("bad un/pw");
}


Regards,
Alexey


Reply all
Reply to author
Forward
0 new messages