Skip to content

Instantly share code, notes, and snippets.

@troelskn
Created March 14, 2010 21:42
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save troelskn/332256 to your computer and use it in GitHub Desktop.
Save troelskn/332256 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;
}
@olleolleolle
Copy link

How about backing off with a doubling timeout? First 1 second, then 2, then 4, etc.

@troelskn
Copy link
Author

Goog idea.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment