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 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘