Всем спасибо, но ни один предложенный вариант не сработал, я понятия не имею в чём разница (с консоли всё выглядит одинаково), но работал только вот такой вариант:
#!/bin/env python2.6
import sys
line = sys.stdin.readline()
while line:
print line,
line = sys.stdin.readline()
Найдётся ли знаток, который объяснит разницу? :)
Вот вызывающая функция php:
function queryGateway ($gwname, $questions)
{
global $racktables_gwdir;
$execpath = "${racktables_gwdir}/{$gwname}/main";
$dspec = array
(
0 => array ("pipe", "r"),
1 => array ("pipe", "w"),
2 => array ("file", "/dev/null", "a")
);
$pipes = array();
$gateway = proc_open ($execpath, $dspec, $pipes);
if (!is_resource ($gateway))
return array ('ERR proc_open() failed in ' . __FUNCTION__);
foreach ($questions as $q)
fwrite ($pipes[0], "$q\n");
fclose ($pipes[0]);
$answers = array ();
while (!feof($pipes[1]))
{
$a = fgets ($pipes[1]);
if (!strlen ($a))
continue;
// Somehow I got a space appended at the end. Kick it.
$answers[] = trim ($a);
}
fclose($pipes[1]);
$retval = proc_close ($gateway);
if ($retval != 0)
throw new RTGatewayError ("gateway failed with code ${retval}");
if (!count ($answers))
throw new RTGatewayError ('no response from gateway');
if (count ($answers) != count ($questions))
throw new RTGatewayError ('protocol violation');
foreach ($answers as $a)
if (strpos ($a, 'OK!') !== 0)
throw new RTGatewayError ("subcommand failed with status: ${a}");
return $answers;
}
а вот этот код вообще вешает страницу с вызовом queryGateway намертво:
#!/bin/env python2.6
import sys
while 1:
x = sys.stdin.readline()
print x,