PHP期望:如何复制流的内容

时间:2015-08-02 17:41:03

标签: php expect

在这个例子中:http://php.net/manual/en/expect.examples-usage.php,即

<?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 ("~$ ", SHELL, EXP_EXACT), // We've got the shell!
    ))) {
        case PASSWORD:
            fwrite ($stream, "secret\n");
            break;
....

我如何输出expect_expectl当前正在处理的$ stream部分?

可能我还没有完全掌握使用流的概念和工具。

我希望看到流的每一行,无论它是否匹配,即使这只是为了我的脚本的调试目的。

1 个答案:

答案 0 :(得分:1)

如果查看official documentation for expect_expectl(),您会看到第三个参数填充了当前匹配。整行都在$match[0]以及以下元素中的匹配段(非常类似于preg_match())。

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 ("~$ ", SHELL, EXP_EXACT), // We've got the shell!
            array (".*", 'ALL', EXP_REGEXP), // Unmatched lines
    ), $match)) {
        case PASSWORD:
            echo $match[0];
            fwrite ($stream, "secret\n");
            break;
        /* other cases */
        case 'ALL':
            echo $match[0];