Forum und email

Expect Functions

소개

This extension allows to interact with processes through PTY. You may consider using the expect:// wrapper with the filesystem functions which provide a simpler and more intuitive interface.

요구 조건

This module uses the functions of the » expect library. You need libexpect version >= 5.43.0.

설치

» PECL 확장은 PHP 배포판에서 제공하지 않습니다. PECL 확장 설치에 관한 정보는 매뉴얼의 PECL 확장 설치에 있습니다. 새 릴리즈, 내려받기, 소스파일, 개발자 정보, CHANGELOG 등의 추가 정보는 여기에 있습니다: » https://pecl.php.net/package/expect.

PHP 4에서 이 PECL 확장의 소스는 PHP 소스의 ext/ 디렉토리 안이나, 위의 PECL 링크에서 찾을 수 있습니다. In order to use these functions you must compile PHP with expect support by using the --with-expect[=DIR] configure option.

Windows users will enable php_expect.dll inside of php.ini in order to use these functions. PHP 4에서 이 DLL 형식은 내려받은 PHP 윈도우 바이너리 안의 extensions/ 디렉토리 안에 있습니다. PECL 확장 DLL» PHP 내려받기 페이지나 » https://pecl4win.php.net/에서 받을 수 있습니다.

실행시 설정

이 함수의 작동은 php.ini 설정에 영향을 받습니다.

In order to configure expect extension, there are configuration options in the configuration file php.ini.

Expect 설정 옵션
이름 기본값 가변성 Changelog
expect.timeout "10" PHP_INI_ALL  
expect.loguser "1" PHP_INI_ALL  
expect.logfile "" PHP_INI_ALL  
PHP_INI_* 상수에 대한 자세한 상세와 정의는 php.ini directives를 참고하십시오.

위 설정 지시어에 대한 간단한 설명입니다.

expect.timeout integer

The timeout period for waiting for the data, when using the expect_expectl() function.

A value of "-1" disables a timeout from occurring.

Note: A value of "0" causes the expect_expectl() function to return immediately.

expect.loguser boolean

Whether expect should send any output from the spawned process to stdout. Since interactive programs typically echo their input, this usually suffices to show both sides of the conversation.

expect.logfile string

Name of the file, where the output from the spawned process will be written. If this file doesn't exist, it will be created.

Note: If this configuration is not empty, the output is written regardless of the value of expect.loguser.

자원형

expect_popen() returns an open PTY stream used by expect_expectl().

예약 상수

이 확장은 다음의 상수들을 정의합니다. 이 확장을 PHP에 내장했거나, 실행시에 동적으로 읽어들일 경우에만 사용할 수 있습니다.

EXP_GLOB (integer)
Indicates that the pattern is a glob-style string pattern.
EXP_EXACT (integer)
Indicates that the pattern is an exact string.
EXP_REGEXP (integer)
Indicates that the pattern is a regexp-style string pattern.
EXP_EOF (integer)
Value, returned by expect_expectl(), when EOF is reached.
EXP_TIMEOUT (integer)
Value, returned by expect_expectl() upon timeout of seconds, specified in value of expect.timeout
EXP_FULLBUFFER (integer)
Value, returned by expect_expectl() if no pattern have been matched.

예제

This example connects to the remote host via SSH, and prints the remote uptime.

Example#1 Expect Usage Example

<?php
ini_set 
("expect.loguser""Off");

$stream fopen ("expect://ssh root@remotehost uptime""r");

$cases = array (
  array (
=> "password:"=> PASSWORD)
);

switch (
expect_expectl ($stream$cases)) {
 case 
PASSWORD:
  
fwrite ($stream"password\n");
  break;
 
 default:
  die (
"Error was occurred while connecting to the remote host!\n");
}

while (
$line fgets ($stream)) {
  print 
$line;
}
fclose ($stream);
?>

The following example connects to the remote host, determines whether installed OS is for 32 or 64 bit, then runs update for specific package.

Example#2 Another Expect Usage Example

<?php
ini_set 
("expect.timeout", -1);
ini_set ("expect.loguser""Off");

$stream expect_popen ("ssh root@remotehost");

while (
true) {
    switch (
expect_expectl ($stream, array (
            array (
"password:"PASSWORD), // SSH is asking for password
            
array ("yes/no)?"YESNO), // SSH is asking whether to store the host entry
            
array ("~$ "SHELLEXP_EXACT), // We've got the shell!
    
))) {
        case 
PASSWORD:
            
fwrite ($stream"secret\n");
            break;

        case 
YESNO:
            
fwrite ($stream"yes\n");
            break;

        case 
SHELL:
            
fwrite ($stream"uname -a\n");
            while (
true) {
                    switch (
expect_expectl ($stream, array (
                            array (
"~$ "SHELLEXP_EXACT), // We've got the shell!
                            
array ("^Linux.*$"UNAMEEXP_REGEXP), // uname -a output
                    
), $match)) {
                        case 
UNAME:
                            
$uname .= $match[0];
                            break;
                        
                        case 
SHELL:
                            
// Run update:
                            
if (strstr ($uname"x86_64")) {
                                    
fwrite ($stream"rpm -Uhv https://mirrorsite/somepath/some_64bit.rpm\n");
                            } else {
                                    
fwrite ($stream"rpm -Uhv https://mirrorsite/somepath/some_32bit.rpm\n");
                            }
                            
fwrite ($stream"exit\n");
                            break 
2;
                    
                        case 
EXP_TIMEOUT:
                        case 
EXP_EOF:
                            break 
2;

                        default:
                            die (
"Error has occurred!\n");
                    }
            }
            break 
2;

        case 
EXP_TIMEOUT:
        case 
EXP_EOF:
            break 
2;

        default:
            die (
"Error has occurred!\n");
    }
}

fclose ($stream);
?>

Table of Contents

  • 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
  • expect_popen — Execute command via Bourne shell, and open the PTY stream to the process