PHP CLI记录交互式SSH会话的输出

I've been trying to sort out recording an interactive SSH session via a PHP CLI script. All I really want is sort of like a tee command, where the entire output is recorded into a string.

I've tried most of the commands to execute external processes to get it to work. They either work ok such as passthru and pcntl_exec but don't let me record the output into a string or array, or they don't display any output to my terminal but still accept stuff from STDIN.

I'm wondering if there is a way of having an interactive ssh session i.e, works normally, but actually records the output for use in PHP.

Thanks

They either work ok such as passthru and pcntl_exec but don't let me record the output into a string or array

Well, you can use a hack to get that string:

ob_start();
passthru('Your command here');
$result=ob_get_clean();

It is bad, but if your server is not high-load - it should work.

<?php
include('Net/SSH2.php');

$ssh = new Net_SSH2('www.domain.tld');
if (!$ssh->login('username', 'password')) {
    exit('Login Failed');
}

echo $ssh->read('username@username:~$');
$ssh->write("ls -la
");
echo $ssh->read('username@username:~$');
?>

You could do something like that.

Try using ObserveIT, it includes a great SSH and Telnet Recorder that cannot be stopped by powerusers