PHP 手册
上一页 |  | 下一页

# XXXV. 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 扩展库安装的一章中找到。 更多信息如新版本，下载，源文件，维护者信息以及更新日志等可以在这里找到： http://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 Windows 执行包中的 `extensions/` 目录下。 可以从 PHP 下载页面或者 http://snaps.php.net/ 下载此 PECL 扩展的 DLL 文件。

# 运行时配置

这些函数的行为受 `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 | "1" | PHP_INI_ALL |
expect.logfile | "" | PHP_INI_ALL |

有关 PHP_INI_* 常量进一步的细节与定义参见附录 H。

以下是配置选项的简要解释。

`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.

# 资源类型

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.

例 1. Expect Usage Example

`<?php
ini_set ("expect.loguser", "Off");
$stream = fopen ("expect://ssh root@remotehost uptime", "r");
$cases = array (
array (0 => "password:", 1 => 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);
?>`

上一页 | 起始页 | 下一页
read_exif_data | 上一级 | expect_expectl