|   | php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login | 
| 
  [2010-09-20 16:55 UTC] sokol at zavolga dot net
 Description:
------------
Script freez on read output via fgets
Reproduce code:
---------------
<?php
ini_set("expect.loguser", "Off");
$stream = fopen("expect://telnet 10.244.0.164", 'r');
$cases = array (
    array (0 => "username:", 1 => USERNAME),
    array (0 => "password:", 1 => PASSWORD),
    array (0 => "#", 1 => PROMPT)
);
while (true) {
    switch (expect_expectl ($stream, $cases)) {
        case PASSWORD:
            fwrite ($stream, "password\r");
            break;
        case USERNAME:
            fwrite ($stream, "admin\r");
            break;
        case PROMPT:
            break 2;
        default:
            die ("Error was occurred while connecting to the remote host!\n");
    }
}
while ($line = fgets($stream)) {
      print $line;
}
fclose ($stream);
?>
Expected result:
----------------
DES-3526 Fast Ethernet Switch Command Line Interface
                            Firmware: Build 5.00-B27
          Copyright(C) 2000-2004 D-Link Corporation. All rights reserved.
username:admin
password:********
DES-3526:admin#
This code do it:
<?php
$stream = fopen("expect://telnet 10.244.0.164", 'r');
$cases = array (
    array (0 => "username:", 1 => USERNAME),
    array (0 => "password:", 1 => PASSWORD),
    array (0 => "#", 1 => PROMPT)
);
while (true) {
    switch (expect_expectl ($stream, $cases)) {
        case PASSWORD:
            fwrite ($stream, "password\r");
            break;
        case USERNAME:
            fwrite ($stream, "admin\r");
            break;
        case PROMPT:
            break 2;
        default:
            die ("Error was occurred while connecting to the remote host!\n");
    }
}
?>
Actual result:
--------------
Freezing without any output
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits             | |||||||||||||||||||||||||||
|  Copyright © 2001-2025 The PHP Group All rights reserved. | Last updated: Fri Oct 31 03:00:01 2025 UTC | 
Please check the following code carefully, it works fine: <?php ini_set("expect.loguser", "Off"); $stream = fopen("expect://telnet localhost", 'r'); $cases = array ( array (0 => "vm-ubuntu login: ", 1 => USERNAME), array (0 => "Password: ", 1 => PASSWORD), array (0 => ":~$ ", 1 => PROMPT) ); while (true) { switch (expect_expectl ($stream, $cases)) { case USERNAME: fwrite ($stream, "michael\n"); break; case PASSWORD: fwrite ($stream, "pass\n"); break; case PROMPT: break 2; default: die ("Error was occurred while connecting to the remote host!\n"); } } fwrite($stream, "exit\n"); while ($line = fgets($stream)) { print $line; } fclose ($stream); ?>