expect_expectl
(PECL expect >= 0.1.0)
expect_expectl — Waits until the output from a process matches one of the patterns, a specified time period has passed, or an EOF is seen
Description
$expect
, array $cases
[, array &$match
] ) : intWaits until the output from a process matches one of the patterns, a specified time period has passed, or an EOF is seen.
If match
is provided, then it is filled with the result of search.
The matched string can be found in match[0]
.
The match substrings (according to the parentheses) in the original pattern can be found
in match[1]
, match[2]
, and so
on, up to match[9]
(the limitation of libexpect).
Parameters
-
expect
-
An Expect stream, previously opened with expect_popen().
-
cases
-
An array of expect cases. Each expect case is an indexed array, as described in the following table:
Expect Case Array Index Key Value Type Description Is Mandatory Default Value 0 string pattern, that will be matched against the output from the stream yes 1 mixed value, that will be returned by this function, if the pattern matches yes 2 integer pattern type, one of: EXP_GLOB
,EXP_EXACT
orEXP_REGEXP
no EXP_GLOB
Return Values
Returns value associated with the pattern that was matched.
On failure this function returns:
EXP_EOF
,
EXP_TIMEOUT
or
EXP_FULLBUFFER
Changelog
Version | Description |
---|---|
0.2.1 |
Prior to version 0.2.1, in match parameter a match string was returned,
not an array of match substrings.
|
Examples
Example #1 expect_expectl() example
<?php
// Copies file from remote host:
ini_set("expect.timeout", 30);
$stream = fopen("expect://scp user@remotehost:/var/log/messages /home/user/messages.txt", "r");
$cases = array(
// array(pattern, value to return if pattern matched)
array("password:", "asked for password"),
array("yes/no)?", "asked for yes/no")
);
while (true) {
switch (expect_expectl($stream, $cases)) {
case "asked for password":
fwrite($stream, "my password\n");
break;
case "asked for yes/no":
fwrite($stream, "yes\n");
break;
case EXP_TIMEOUT:
case EXP_EOF:
break 2; // break both the switch statement and the while loop
default:
die "Error has occurred!";
}
}
fclose($stream);
?>
English translation
You have asked to visit this site in English. For now, only the interface is translated, but not all the content yet.If you want to help me in translations, your contribution is welcome. All you need to do is register on the site, and send me a message asking me to add you to the group of translators, which will give you the opportunity to translate the pages you want. A link at the bottom of each translated page indicates that you are the translator, and has a link to your profile.
Thank you in advance.
Document created the 30/01/2003, last modified the 26/10/2018
Source of the printed document:https://www.gaudry.be/en/php-rf-expect-expectl.html
The infobrol is a personal site whose content is my sole responsibility. The text is available under CreativeCommons license (BY-NC-SA). More info on the terms of use and the author.
References
These references and links indicate documents consulted during the writing of this page, or which may provide additional information, but the authors of these sources can not be held responsible for the content of this page.
The author This site is solely responsible for the way in which the various concepts, and the freedoms that are taken with the reference works, are presented here. Remember that you must cross multiple source information to reduce the risk of errors.