Skip to content
Alexey1Gavrilov edited this page Sep 24, 2014 · 54 revisions

Expectit comes with three custom Ant tasks for performing expect operations on the standard input and output streams of spawn processes, sockets and SSH services.

You can go directly to the self-explanatory Ant build file example which covers most of the use cases.

Installation

The expect Ant tasks are defined as an antlib resource in the expectit-ant.jar file. The file can be downloaded from the project release page or from the Maven central. Note that the Ant support has been added in version 0.3.0.

Here is an example of the taskdef element that adds the expect tasks definitions to the current project with the given XML namespace URI.

 <taskdef classpath="${expectit-ant.jar}"
             resource="net/sf/expectit/ant/antlib.xml" uri="antlib:net.sf.expectit.ant" />
 <target name="main" xmlns:expect="antlib:net.sf.expectit.ant">
    <expect:exec executable="/bin/sh">
      <expect:sequential>
        <expect:send line="exit"/>
      </expect:sequential>
    </expect:exec>
 </target>

For more information about the task definition please refer to the Ant taskdef documentation. The expect ssh task depends on external library which is not included in the Ant distribution. For more information please refer to the Ant sshexec task documentation.

Common Parameters

All three tasks share same set of parameters to configure an Expect instance.

Attributes

Attribute Description Required
expectTimeout The timeout for expect operations in milliseconds. Defaults to 30 seconds. No
charset The charset used to convert bytes to characters. Defaults to the system local default. No
errorOnTimeout Stops when the first expect operation times and exits with error status. Defaults to false. No
lineSeparator The default line separator for send. Defaults to "\n". No
echoOutput Enables logging of the input and output streams by the Ant's logger. Defaults to false. No

Nested Container Elements

Element Description Required
filters The container for the input filter elements applied for the Expect instance No, but if present must not be empty
sequential The container for the expect elements as well as any Ant tasks that will be executed sequentially. Yes

Filter Elements

All filter elements are optional.

Filter Description Attributes
removeColors Removes [ANSI escape sequences for colors] (http://en.wikipedia.org/wiki/ANSI_escape_code#Colors) from the input streams No
removeNonPrintable Removes all the non-printable characters expect white spaces No
replaceInString Replaces every substring in the input string that matches the given regular expression and replaces it with given replacement regexp - the regular expression
replacement - the string to be substituted for each match
replaceInBuffer Replaces every substring in the entire input buffer that matches the given regular expression and replaces it with given replacement Same as above

Sequential Expect Tasks

The sequential element can contains any Ant tasks that will be executed sequentially. The following table describes the expect specific tasks.

Tasks Description Attributes
send Sends string data to the output stream string - a string to be passed to the output stream
line - a string to be passed to the output stream and followed by the line separator
contains Matches when the examined input contains the given string string - the string to find
exact Matches when the examined input is equal to the given string As of above
regexp Matches when the examined input contains the given regular expression regexp - the regular expression to find.
Common expect attributes
matches Matches when the examined input matches the given regular expression regexp - the regular expression to match
Common expect attributes
anyString Matches if the input contains any characters Common expect attributes
eof Matches is the input has been closed Common expect attributes
times A container element for another matcher. Matches when the child matcher matches the given number of times. number - the number of times the child matcher must match
Common expect attributes
Note that he result of the child matcher is exported with the property prefix.0.
anyOf A container element for other matchers. Matches when any of the child matcher matches. Common expect attributes
Note that he result of the child matcher is exported with the property prefix.index, where the index is the number of the child element.
allOf A container element for other matchers. Matches when any of the child matcher matches. As of above
sequence A container element for other matchers. Matches when the child matchers matche sequencially. As of above

Common Expect Attributes

All the common attributes are optional.

Attribute Description
input The number of the input stream. Defaults to 0.
timeout The timeout in milliseconds of the expect operation. Defaults to the global expect timeout
resultPrefix The optional prefix for the properties that will be set as the result of the successful expect operation.
The properties key/value format is as follows:
prefix.before - the part of the input string from the beginning until the starting position of the match
prefix.group - the matching string
prefix.success - true
prefix.group.number - the group captured as the result of the match, where the number is the group index.

Expect Exec Task

The expect exec task inherits all the parameters from the standard Ant exec task. The only exception the redirector nested element which is not support. In addition to the common expect parameters it has their own:

Attribute Description Required
destroyProcess Sets the flag indicating if the underlying process should be destroyed after the task is finished. Defaults to false. No

Example

Here is an example interacting with a spawn Windows shell process.

    <target name="windows" xmlns:ex="antlib:net.sf.expectit.ant">
        <ex:exec executable="cmd" charset="cp866">
            <ex:filters>
                <ex:replaceInString regexp="Microsoft" replacement="XXX"/>
            </ex:filters>
            <ex:sequential>
                <ex:send line="echo hello!"/>
                <ex:contains string="hello!" resultPrefix="result"/>
                <echoproperties prefix="result" />
                <ex:send line="exit"/>
                <ex:eof/>
            </ex:sequential>
        </ex:exec>
    </target>

Expect Socket Task

In addition to the common expect parameters it has their own:

Attribute Description Required
host The required host name or internet address Yes
port The required port Yes

Here is an example interacting with a public FTP service:

    <target name="socket" xmlns:ex="antlib:net.sf.expectit.ant">
        <ex:socket host="ftp.freebsd.org" port="21" echooutput="true">
            <ex:sequential>
                <ex:send line="USER ftp"/>
                <ex:anyString />
                <ex:send line="PASS anonymous"/>
                <ex:anyString/>
                <ex:send line="HELP"/>
                <ex:times number="2">
                    <ex:contains string="214"/>
                </ex:times>
            </ex:sequential>
        </ex:socket>
    </target>

Expect SSH Task

The expect SSH task inherits most of the parameters of the the standard Ant SSH session task with the exception of the command attribute. It also supports the common expect parameters.

Here is an example of interacting with a public SSH service:

    <taskdef classpath="${expectit.jar}"
             resource="net/sf/expectit/ant/antlib.xml" uri="antlib:net.sf.expectit.ant" />

    <target name="main" xmlns:expect="antlib:net.sf.expectit.ant">
        <expect:ssh host="sdf.org" errorOnTimeout="true" username="new" echoOutput="true">
            <expect:filters>
                <expect:removeColors/>
                <expect:removeNonPrintable/>
            </expect:filters>
            <expect:sequential>
                <expect:contains string="[RETURN]"/>
                <expect:send line=""/>
                <expect:contains string="login:"/>
                <expect:send line="new"/>
                <expect:contains string="(Y/N)"/>
                <expect:send string="N"/>
                <expect:regexp regexp=": $"/>
                <expect:send string="\b"/>
                <expect:regexp regexp="\(y\/n\)"/>
                <expect:send line="y"/>
                <expect:contains string="Would you like to sign the guestbook?"/>
                <expect:send line="n"/>
                <expect:contains string="[RETURN]"/>
            </expect:sequential>
        </expect:ssh>
    </target>