Forum und email
Expect Functions

XXXV. Expect Functions

Úvod

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.

Požadavky

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

Instalace

Toto rozšíření PECL není přibaleno k PHP. Informace o instalaci tohoto PECL rozšíření lze najít v kapitole manuálu nazvané Instalace rozšíření PECL. Další informace, jako nové verze, soubory ke stažení, zdrojové soubory, správcovské informace a CHANGELOG, lze najít zde: https://pecl.php.net/package/expect.

V PHP 4 lze zdroje k těmto rozšířením PECL najít v adresáři ext/ ve zdrojích PHP source or nebo přes výše uvedený odkaz na 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. V PHP 4 je tato DLL knihovna umístěna v adresáři extensions/ v downloadu binárních souborů PHP pro Windows. Tuto DLL knihovnu pro rozšíření PECL můžete stahovat ze stránky PHP Downloads nebo z https://snaps.php.net/.

Konfigurace běhu

Chování těchto funkcí je ovlivněno nastavením parametrů v php.ini.

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

Tabulka 1. Expect Konfigurační volby

JménoVýchozíMěnitelnéChangelog
expect.timeout"10"PHP_INI_ALL 
expect.loguser"1"PHP_INI_ALL 
expect.logfile""PHP_INI_ALL 
Pro další detaily a definice konstant PHP_INI_*, viz dokumentace k ini_set().

Zde je stručný popis konfiguračních direktiv.

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.

Poznámka: 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.

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

Typy prostředků

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

Předdefinované konstanty

Tyto konstanty jsou definovány tímto rozšířením a budou k dispozici pouze tehdy, bylo-li rozšíření zkompilováno společně s PHP nebo dynamicky zavedeno za běhu.

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.

Příklady

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

Příklad 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);
?>
Obsah
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