Skip to content

Instantly share code, notes, and snippets.

@hmert
Forked from troelskn/gist:332256
Created December 30, 2010 23:25
Show Gist options
  • Save hmert/760479 to your computer and use it in GitHub Desktop.
Save hmert/760479 to your computer and use it in GitHub Desktop.
/**
* Wrapper to execute a remote command over ssh.
* @param $ssh Connection from ssh2_connect
* @param $command string to execute on shell
* @returns string
*/
function ssh2_exec_command($ssh, $command, $retry = 5) {
// The `sleep` is a nasty hack to account for some kind of timing bug in ssh2
$sleep = 0;
$stream = false;
while (!$stream) {
$stream = ssh2_exec($ssh, "sleep {$sleep} ; " . $command);
$sleep = $sleep + 2;
if ($sleep > 60) {
throw new Exception("Unable to execute command");
}
}
stream_set_timeout($stream, 10);
stream_set_blocking($stream, true);
$data = stream_get_contents($stream);
fclose($stream);
if (!$data && $retry > 0) {
// On failure, retry
return ssh2_exec_command($command, $retry - 1);
}
return $data;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment