php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #59431 Freez on fetching output via fgets, fread etc.
Submitted: 2010-09-20 16:55 UTC Modified: 2011-01-09 03:51 UTC
From: sokol at zavolga dot net Assigned:
Status: No Feedback Package: expect (PECL)
PHP Version: 5.3.2 OS: Linux 2.6.34.7 (OpenSUSE)
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: sokol at zavolga dot net
New email:
PHP Version: OS:

 

 [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

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2010-10-10 09:18 UTC] spektom at gmail dot com
Should telnet session print something that you're waiting for in a while loop? What I mean is that after the first while() you've logged into the remote host. Then telnet is waiting for your command, isn't it?
 [2010-10-10 09:30 UTC] spektom at gmail dot com
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);

?>
 [2011-01-09 03:51 UTC] spektom at gmail dot com
No feedback was provided. The bug is being suspended because
we assume that you are no longer experiencing the problem.
If this is not the case and you are able to provide the
information that was requested earlier, please do so and
change the status of the bug back to "Open". Thank you.


 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Apr 16 17:01:30 2024 UTC