Getting timeout on matcher when match appears to be there.

512 views
Skip to first unread message

Jon August

unread,
Feb 23, 2016, 11:42:06 AM2/23/16
to Yet another Expect for Java
Hi,

I'm getting this exception when running my application:

jaugust>./multicastSubscriberLookup.sh
Connecting to database...
FAILURE
- hudson3router - net.sf.expectit.ExpectIOException: Expect operation fails (timeout: 30000 ms) for matcher: contains('#')
Execution finished.


However, here's my code:

        JSch jSch = new JSch();
       
try {
           
Session session = jSch.getSession(username, h.hostname);
            session
.setPassword(password);
           
Properties config = new Properties();
            config
.put("StrictHostKeyChecking", "no");
            session
.setConfig(config);
            session
.connect();
           
Channel channel = session.openChannel("shell");


           
StringBuilder wholeBuffer = new StringBuilder();


           
Expect expect = new ExpectBuilder()
                   
.withOutput(channel.getOutputStream())
                   
.withInputs(channel.getInputStream(), channel.getExtInputStream())
                   
.withEchoOutput(wholeBuffer)
                   
.withEchoInput(wholeBuffer)
                   
.withExceptionOnFailure()
                   
.build();


            channel
.connect();


           
MultiResult multiResult = expect.expect(anyOf(contains(">"), contains("#")));
           
if (multiResult.getResults().get(0).isSuccessful()) {
                expect
.sendLine("enable");
                expect
.expect(contains("assword:"));
                expect
.sendLine(password);
                expect
.expect(contains("#"));
           
}


           
//APPLY
            expect
.sendLine("show ip igmp snooping groups");
            expect
.expect(contains("#"));
            expect
.sendLine("show ip igmp snooping explicit-tracking");
            expect
.expect(contains("#"));


            expect
.sendLine("exit");


           
String response = wholeBuffer.toString();
           
this.setResponse(h.runSetId, response);


            channel
.disconnect();
            session
.disconnect();
            expect
.close();


       
} catch (Exception e) {
           
System.out.println("FAILURE - " + h.hostname + " - " + e);
       
}

and this is what it looks like when I log in manually:

jaugust> ssh admin@hudson3router
+-----------------------------------------------------------------------+
|                               WARNING                                 |
|                               -------                                 |
|                                                                       |
| The confidential programs and data on this system are either licensed |
| to or owned by the company (the "Company") operating this system, or  |
| its affiliates. This is a private system, intended to be accessed and |
| used only by the Company's authorized users. Any other access to or   |
| use of this system is unlawful and strictly prohibited. This system   |
| may be monitored at any time for operational reasons. If you are not  |
| an user authorized by the Company to access this system, you must     |
| DISCONNECT IMMEDIATELY. DO NOT ATTEMPT TO LOG IN.                     |
|                                                                       |
+-----------------------------------------------------------------------+
Password: 
Last login: Tue Feb 23 11:09:24 2016 from 10.1.1.120
hudson3router#

It should be matching the # prompt.  The strange part is that this consistently works fine on some devices, and consistently not on others like this one and I'm not seeing any differences between the devices.  For example, here's another device where it works fine:

jaugust> ssh admin@hudson8router
+-----------------------------------------------------------------------+
|                               WARNING                                 |
|                               -------                                 |
|                                                                       |
| The confidential programs and data on this system are either licensed |
| to or owned by the company (the "Company") operating this system, or  |
| its affiliates. This is a private system, intended to be accessed and |
| used only by the Company's authorized users. Any other access to or   |
| use of this system is unlawful and strictly prohibited. This system   |
| may be monitored at any time for operational reasons. If you are not  |
| an user authorized by the Company to access this system, you must     |
| DISCONNECT IMMEDIATELY. DO NOT ATTEMPT TO LOG IN.                     |
|                                                                       |
+-----------------------------------------------------------------------+
Password: 
Last login: Tue Feb 23 11:04:27 2016 from 10.1.1.120
hudson8router#

Any ideas why the first one times out and the second works great?  Is there some more logging I can do to pinpoint the issue?

Thanks,
   -Jon

Alexey Gavrilov

unread,
Feb 23, 2016, 2:29:52 PM2/23/16
to Jon August, Yet another Expect for Java
Hi!

On which line the exception occurs? Can you print the stack trace?

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

Jon August

unread,
Feb 23, 2016, 3:40:33 PM2/23/16
to Yet another Expect for Java, jona...@gmail.com, Alexey1....@gmail.com
Ah, your question helped me figure out the problem.

The exception occurs after the first show command.  The problem is that on these devices, anything over a certain number of pages is paginated.  It was waiting for me to hit enter before showing me the rest of the output.

I was able to fix this by adding this before my first "show" command:

            expect.sendLine("terminal length 0");
            expect.expect(contains("#"));

Thank you very much, again, for all your help.

-Jon

Alexey Gavrilov

unread,
Feb 23, 2016, 3:41:21 PM2/23/16
to Jon August, Yet another Expect for Java
You welcome :)

atul dubile

unread,
Nov 10, 2022, 7:01:45 AM11/10/22
to Yet another Expect for Java
How to send Enter Key in sendline()?

my application needs to enter key press to select an option from the menu.
Does anyone know the solution, please let me know.

Note:- here I didn't want a new line (\n) I need to press Enter Key

Jon August

unread,
Nov 10, 2022, 7:05:37 AM11/10/22
to Yet another Expect for Java, atul dubile
We use \r with success
You received this message because you are subscribed to a topic in the Google Groups "Yet another Expect for Java" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/java-expectit/m66qjx03Uik/unsubscribe.
To unsubscribe from this group and all its topics, send an email to java-expecti...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/java-expectit/7cf5b0a5-8fd1-4c92-ba3c-5b1398431da2n%40googlegroups.com.

atul dubile

unread,
Nov 10, 2022, 7:09:09 AM11/10/22
to Yet another Expect for Java
Hi Jona,

Thanks for the replay.

I am just confirming, the below is the correct way, right?

expect.sendLine(" \r  ");

right?

Jon August

unread,
Nov 10, 2022, 7:39:44 AM11/10/22
to Yet another Expect for Java, atul dubile
Yes, for example in a Cisco device we would do:
expect.sendLine(“copy run start\r”);

This has the effect of hitting enter when the device asks:

Are you sure Yes/No [Y]:

The extra enter submits the default of yes and tells the device to save. 

Hope that helps. 

Jon

atul dubile

unread,
Nov 11, 2022, 12:54:36 AM11/11/22
to Yet another Expect for Java
Hi Jona,

Thanks, it's working. 

I also have the below query. 
need to
1. Send "Space Key" in sendline()
2. Send "up Arrow Key" in sendline()
3. send "Down Arrow Key" in sendline()

can someone please help me here as well?

Thanks



atul dubile

unread,
Nov 15, 2022, 2:02:14 AM11/15/22
to Yet another Expect for Java
1. Send "Space Key" in sendline()
2. Send "up Arrow Key" in sendline()
3. send "Down Arrow Key" in sendline()

can someone please help me here as well?


Reply all
Reply to author
Forward
0 new messages