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.
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];