Expect sending string twice

102 views
Skip to first unread message

prati...@gmail.com

unread,
May 15, 2014, 10:10:10 AM5/15/14
to java-e...@googlegroups.com
Code::
JSch jSch = new JSch();
Session session = jSch.getSession("admin", "192.168.0.233");
session.setPassword("vnera123");
session.setConfig("StrictHostKeyChecking", "no");
session.connect();
Channel channel = session.openChannel("shell");

Expect expect = new ExpectBuilder()
.withOutput(channel.getOutputStream())
.withInputs(channel.getInputStream(), channel.getExtInputStream())
.withEchoOutput(adapt(System.out))
.withInputFilters(removeColors(), removeNonPrintable())
.withErrorOnTimeout(true)
.build();
channel.connect();
expect.expect(contains("R1>"));
expect.sendLine();
expect.sendLine("enable");
expect.expect(contains("Password: "));
expect.sendLine("abcde");
channel.disconnect();
session.disconnect();
expect.close();

Output:
1>
enable

R1>
R1>enable
Password:
% Access denied

R1>abcde
---------------------------------------------------------------------
The "enable" command and also the password seems to be sent twice.
Is this a bug or am I going wrong somewhere.
I am using the latest jar(expectit-core-0.3.1.jar).

Thank-You,
Pratik

Alexey Gavrilov

unread,
May 15, 2014, 12:59:46 PM5/15/14
to prati...@gmail.com, java-e...@googlegroups.com
Hi,

Hmm. In many other tests all the commands are clearly sent once. Perhaps your service echos the received command. 

To check that I suggest to separate the output of what was actually being sent from what was received. For example,  when constructing an Expect instance you can pass the following sample anonymous EchoOutput class:

==========
          .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);
                    }
                })
============

Then that can give a clue of what was sent and what was received.

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.

Sebastian YEPES

unread,
Jul 3, 2014, 6:46:49 PM7/3/14
to java-e...@googlegroups.com, prati...@gmail.com, alexey1....@gmail.com
It would be nice to update this debugging tip using the new input and output methods (withEchoOutput/withEchoInput)

Best regards,
Sebastian

Alexey Gavrilov

unread,
Jul 4, 2014, 1:38:19 AM7/4/14
to Sebastian YEPES, java-e...@googlegroups.com, prati...@gmail.com
Done.

Regards,
Alexey

Sebastian YEPES

unread,
Jul 4, 2014, 8:09:42 AM7/4/14
to java-e...@googlegroups.com, sye...@gmail.com, prati...@gmail.com, alexey1....@gmail.com
Ok thanks.
Hoy sould it work now to only capture the received data? compared to the prevues method:

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



Alexey Gavrilov

unread,
Jul 4, 2014, 8:34:47 AM7/4/14
to Sebastian YEPES, java-e...@googlegroups.com, prati...@gmail.com
Hi!

Now you register an appender separately for input and output data. For example:

    …
    .withEchoOutput(System.out)
    .withEchoInput(System.err, …)
    …

The number of input appenders should correspond to the number of input streams.

The method accepts an instance of Appendable which quite flexible. You can pass a StringBuilder for example.

Regards,
Alexey
Reply all
Reply to author
Forward
0 new messages