Web lists-archives.com

Re: [PHP] expect_expectl: how to show the part of the stream that it is matching against

Hi all,

On 02-Aug-15, jm+php-general@xxxxxxx wrote:
On 02-Aug-15 20:01, Aziz Saleh wrote:

If you want what is coming from the server, you would store the results of expect_expectl in a variable and output it. Unless you mean something else by the buffer!
AFAICS there is no way to store the result as there is no result per se.

There is only the $match variable which is empty if there was no match.

Also, defining a wildcard match in $cases proves unsuccessful as you can't
order thoses cases, meaning if you simply put a "*" match there that you'd
think would act as a fallback, then it always matches that and none of the
other patterns anymore.

Furthermore, in the "default" case of the switch statement, the $match
variable doesn't seem to be populated at all.

Finally, it doesn't seem to perform the pattern lookup line by line,
otherwise the default case would be hit way more often in my case.

I still haven't made headway.

Since matching a wilcard doesn't work as described above, I tried what they did here (https://code.google.com/p/networkautomation/source/browse/trunk/bin/expect.php?r=1) which is trying to match "(.....)", that is, not a wildcard but 1024 or 2048 characters, and buffer the contents that way on their way to the prompt.

---------- START CODE --------

  $buffer = "";
  $dots   = str_repeat(".", 2048);

  $cases = array(
    array(0=>$prompt,   1=>'PROMPT', 2=>EXP_REGEXP),
    // a real wilcard match (.*) doesn't work because it always matches
// and all other things never will, therefore try to capture a limited amount
    array(0=>"($dots)", 1=>'BUFFER', 2=>EXP_REGEXP)

  while (true) {
    switch (expect_expectl($stream, $cases, $match)) {
      // for some reason, this case has to be first....
      case 'BUFFER':
  echo "\n\n ______________________ BUFFERING ____________________ \n\n";
        $buffer .= $match[1];

      // Note: We don't have to check any specific precondition
      // Having the prompt is precondition enough to continue working
      case 'PROMPT':
  echo "\n\n _____________________ FOUND PROMPT __________________ \n\n";
        $buffer .= $match[0];

---------- END CODE --------

I must say that doesn't work either in my case. It matches the BUFFER case once, and then goes to PROMPT, even if there was no prompt that I could see.

Basically I'd like to be able to process each line of the stream regardless if it matches the prompt or not.

Is anyone actually using this built-in expect thingy successfully or am I too stupid to use it correctly?

$ php -v
PHP 5.3.3-7+squeeze26 with Suhosin-Patch (cli) (built: Apr 29 2015 09:25:24)
Copyright (c) 1997-2009 The PHP Group
Zend Engine v2.3.0, Copyright (c) 1998-2010 Zend Technologies
    with Xdebug v2.1.0, Copyright (c) 2002-2010, by Derick Rethans
    with Suhosin v0.9.32.1, Copyright (c) 2007-2010, by SektionEins GmbH


PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php