note 120268 added to function.ssh2-exec
| From: | edoceo at gmail dot com | Date: | Sat, 03 Dec 2016 20:46:46 +0000 |
| Subject: | note 120268 added to function.ssh2-exec | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-207968@lists.php.net to get a copy of this message | ||
Here's what I use for doing ssh2_exec for long running scripts, expanding on other examples.
It waits for a timeout and checks if both streams are finished. Does not use blocking and it gets
data from both STDOUT and STDERR.
<?php
$stdout = ssh2_exec($ssh, $cmd);
$stderr = ssh2_fetch_stream($stdout, SSH2_STREAM_STDERR);
if (!empty($stdout)) {
$t0 = time();
$err_buf = null;
$out_buf = null;
// Try for 30s
do {
$err_buf.= fread($stderr, 4096);
$out_buf.= fread($stdout, 4096);
$done = 0;
if (feof($stderr)) {
$done++;
}
if (feof($stdout)) {
$done++;
}
$t1 = time();
$span = $t1 - $t0;
// Info note
echo "while (($tD < 20) && ($done < 2));\n";
// Wait here so we don't hammer in this loop
sleep(1);
} while (($span < 30) && ($done < 2));
echo "STDERR:\n$err_buf\n";
echo "STDOUT:\n$out_buf\n";
echo "Done\n";
} else {
echo "Failed to Shell\n";
}
?>
Adjust your timeout as necessary.
----
Server IP: 72.52.91.14
Probable Submitter: 75.172.49.36
----
Manual Page -- http://php.net/manual/en/function.ssh2-exec.php
Edit -- https://master.php.net/note/edit/120268
Del: integrated -- https://master.php.net/note/delete/120268/integrated
Del: useless -- https://master.php.net/note/delete/120268/useless
Del: bad code -- https://master.php.net/note/delete/120268/bad+code
Del: spam -- https://master.php.net/note/delete/120268/spam
Del: non-english -- https://master.php.net/note/delete/120268/non-english
Del: in docs -- https://master.php.net/note/delete/120268/in+docs
Del: other reasons-- https://master.php.net/note/delete/120268
Reject -- https://master.php.net/note/reject/120268
Search -- https://master.php.net/manage/user-notes.php