债务

Expect 扩展允许 使用PTY与进程交互。

你可以认为使用 expect:// wrapper 协议 来提供更简单更直观的文件系统功能。

expect_popen

      通过Burne shell执行命令,并将PtY流打开到进程中。

expect_expectl

      等待 进程 的输出,来匹配一个预期。要么指定时间到,要么已经有结束的标示(EOF)。

如果提供匹配,则填充搜索结果。匹配的字符串可以在匹配[ 0 ]中找到。原始模式中的匹配子字符串(根据括号)可以在匹配[1]、匹配[2]等中找到,直到匹配[9](libexpect的限制) 

扫描二维码关注公众号,回复: 3004126 查看本文章

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

猜你喜欢

转载自www.cnblogs.com/zhco/p/9572070.html