note 87745 modified in function.ssh2-exec by danbrown
| From: | danbrown@php.net | Date: | Sun, 18 Sep 2011 01:46:12 +0000 |
| Subject: | note 87745 modified in function.ssh2-exec by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-183243@lists.php.net to get a copy of this message | ||
I believe most of the problem that people are having here is that there is a misconception about
what blocking *really* means.
Blocking means a read from the *stream* will wait until there is data. Not necessarily all the data
from the application -- but *some*. So it won't help you at all if you're executing a
command that doesn't write to stdout, or writes a whole lot of data.
So there are 2 problems:
1. If you need to know that a silent program is done via ssh2_exec, you'll need to signal it to
yourself. ssh2_exec will *not* block execution until the command is done executing. And 2
consecutive ssh2_execs may execute asynchronously. You could also log into a shell via ssh2_shell
and parse up to the next prompt -- but that's overkill. You can also do this by adding on some
sort of sentinel at the end of your command, such as echo "@," and then block on reads
until you see a "@." Ensure "@" won't appear in the output, or escape the
output via some encoding mechanism if you can't do that.
2. If the program takes awhile, you have the same problem. You need to read until you're done.
So you need a sentinel value like the above.
3. Sleep() is just a bad idea here. Commands rarely take the same amount of time to execute a
command twice. It may be OK if you're doing *one* thing and can just wait 5 seconds. But
that's not cool if it's something you're doing in a loop.
Hope this helps!
--was--
I believe most of the problem that people are having here is that there is a misconception about
what blocking *really* means.
Blocking means a read from the *stream* will wait until there is data. Not necessarily all the data
from the application -- but *some*. So it won't help you at all if you're executing a
command that doesn't write to stdout, or writes a whole lot of data.
So there are 2 problems:
1. If you need to know that a silent program is done via ssh2_exec, you'll need to signal it to
yourself. ssh2_exec will *not* block execution until the command is done executing. And 2
consecutive ssh2_execs may execute asynchronously. You could also log into a shell via ssh2_shell
and parse up to the next prompt -- but that's overkill. You can also do this by adding on some
sort of sentinel at the end of your command, such as echo "@," and then block on reads
until you see a "@." Ensure "@" won't appear in the output, or escape the
output via some encoding mechanism if you can't do that.
2. If the program takes awhile, you have the same problem. You need to read until you're done.
So you need a sentinel value like the above.
3. Sleep() is just a bad idea here. Commands rarely take the same amount of time to execute a
command twice. It may be OK if you're doing *one* thing and can just wait 5 seconds. But
that's not cool if it's something you're doing in a loop.
I wrote a wrapper here --
http://www.seoegghead.com/software/ssh2-php-wrappers.seo
Use it -- or get ideas from it. I hope this saves someone a bad day.
Hope this helps!
http://php.net/manual/en/function.ssh2-exec.php