-
-
Save hmert/760479 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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