Приглашаем посетить
Григорьев А.А. (grigoryev.lit-info.ru)

Expect Functions


XXXIV. 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. Дополнительная информация, такая как новый версии, скачивание, исходные файлы, информация о разработчике и CHANGELOG, могут быть найдены здесь: http://pecl.php.net/package/expect.

В PHP 4 исходные файлы этого расширения PECL могут быть найдены в директории ext/ внутри исходных файлов PHP или по ссылке 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 находится в директории extensions/ внутри директории бинарного дистрибутива PHP для Windows. Вы можете скачать DLL этого расширения PECL со страницы PHP Downloads или http://snaps.php.net/.

Настройка во время выполнения

Поведение этих функций зависит от установок в php.ini.

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

Таблица 1. Expect Опции настройки

ИмяПо умолчаниюМеняемоChangelog
expect.timeout"10"PHP_INI_ALL 
expect.loguser"On"PHP_INI_ALL 
expect.logfile""PHP_INI_ALL 
Для подробного описания констант PHP_INI_*, обратитесь к документации функции ini_set().

Краткое разъяснение конфигурационных директив.

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.

Замечание: 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.

Замечание: If this configuration is not empty, the output is written regardless of the value of expect.loguser.

Типы ресурсов

Данное расширение не определяет никакие типы ресурсов.

Предопределенные константы

Перечисленные ниже константы определены данным расширением и могут быть доступны только в том случае, если 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.

Пример 1. Expect Usage Example

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

$stream popen ("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);
?>
Содержание
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 -- Exectute command via Bourne shell, and open the PTY stream to the process