Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting SSH output "as it happens" via PHP?

Tags:

php

I've used libraries like Fabric (Python) to perform various tasks on a remote server. When I run tasks via this library, I get the output from the remote server as the actions are happening. For example, if the task is executing a git pull on the server, I get the line by line output of that as it happens.

However, when I look at the various PHP SSH libs and the SSH2 extension. It appears there is only a way to get ALL of the output of from the command after it has already happened, in one long string. I would like to be fed output back from the remote server as the task is running. Is this possible? In pseudocode, this is kind of what I'm looking for:

Server::run('git pull origin master', function($output)
{
     echo $output.PHP_EOL;
});
like image 612
TaylorOtwell Avatar asked May 06 '13 00:05

TaylorOtwell


1 Answers

Got this working. You can just read the stream returned by ssh2_exec using fgets in a loop.

like image 66
TaylorOtwell Avatar answered Nov 07 '22 15:11

TaylorOtwell