dongling4288 2015-08-02 17:41
浏览 58

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

In this example: http://php.net/manual/en/expect.examples-usage.php, i.e.

<?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
");
            break;
....

How would I output the part of $stream that expect_expectl is currently working on?

Probably I haven't completely grasped the concepts and tools to work with streams.

I want to see each line of the stream regardless if it matches or not even if that's only for debugging purposes of my script.

  • 写回答

1条回答 默认 最新

  • duanchi0883649 2015-08-02 17:58
    关注

    If you look at the official documentation for expect_expectl(), you'll see that it takes a third argument which is populated with the current match. The entire line would be in $match[0] and matched segments in the following elements (much like 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
    ");
                break;
            /* other cases */
            case 'ALL':
                echo $match[0];
    
    评论

报告相同问题?

悬赏问题

  • ¥15 一道python难题
  • ¥15 用matlab 设计一个不动点迭代法求解非线性方程组的代码
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试
  • ¥20 问题请教!vue项目关于Nginx配置nonce安全策略的问题
  • ¥15 教务系统账号被盗号如何追溯设备
  • ¥20 delta降尺度方法,未来数据怎么降尺度