expect_expectl

(PECL expect >= 0.1.0)

expect_expectlWaits until the output from a process matches one of the patterns, a specified time period has passed, or an EOF is seen

说明

expect_expectl ( resource $expect , array $cases [, array &$match ] ) : int

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

参数

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 or EXP_REGEXP no EXP_GLOB

返回值

Returns value associated with the pattern that was matched.

On failure this function returns: EXP_EOF, EXP_TIMEOUT or EXP_FULLBUFFER

更新日志

版本 说明
0.2.1 Prior to version 0.2.1, in match parameter a match string was returned, not an array of match substrings.

范例

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);
?>

参见

  • expect_popen() - Execute command via Bourne shell, and open the PTY stream to the process

相关文章
php apd 函数 stops the interpreter and waits on a cr from the socketphp cubrid 过时的别名和函数 read data from glo and send it to std outputphp date/time 函数 get info about given date formatted according to the specified formatphp expect 函数 waits until the output from a process matches one of the patterns a specified time period has passed or an eof is seenphp expect 函数 execute command via bourne shell and open the pty stream to the processphp mcve 函数 delete specified transaction from mcve conn structurephp 多字节字符串 函数 convert kana one from another zen kaku han kaku and more php newt 函数 replaces the current help line with the one from the stackphp postgresql 函数 submits a command to the server and waits for the result with the ability to pass parameters separately from the sql command textphp runkit 函数 specify a function to capture and/or process output from a runkit sandboxphp 杂项 函数 convert string from one codepage to anotherphp snmp 函数 return all objects including their respective object id within the specified onephp stream 函数 copies data from one stream to anotherphp swoole 函数 trigger a one time callback function in the futurephp date/time 函数 returns the timezone name from abbreviationphp trader 函数 lowest value over a specified periodphp trader 函数 index of lowest value over a specified periodphp trader 函数 lowest and highest values over a specified periodphp trader 函数 indexes of lowest and highest values over a specified periodphp sam 函数 read one or more messages from a queue without removing it from the queue
关注编程学问公众号