I ran in to many ssh2 stream problems as well.
Problem 1. One problem I think related to the openssl version I was using. I was using the standard version with Fedora Core 3. ssh connected actions sometimes worked and sometimes did not work.
I recompiled the ssh2.so with the following versions.
libssh2-0.8 openssl-0.9.7g ssh2-0.7
Problem 2. At this point file transfers worked great. However, running commands and reading the stream was having problems.
Using the classic
<? | while (!feof($ssh_stream)) { $contents .= fread($||ssh_stream||, 5120); } ?>
I found that I never got any responses.
After testing I found that my $||ssh_stream always had a true response to feof($||ssh_stream||)|;
And that fread($ssh_stream,5120) did not always bring back data.
While the following is not a Correction to the problem, it does allow you to read the incoming stream
and know when it is done computing. This is the function I use which is in side a remoteConnection
class I have wrapping the different ssh2 functions. If somebody comes up with a "CORRECTIOIN" to
the problem rather than a work around, I would surely appreciate it.
Here is my work around with comments. Hope it helps.
<?
function command ($command) {
// attach "echo '__end_cmd_response__';" to the end of any command run.
// this will be the last thing in the streams output so I can identify when my command
// has finished.
$stream = ssh2_exec($this->conn, $command ."\necho '__end_cmd_response__';" );
if ($stream) {
// not fully sure why I do this. but it does not seem to hurt. The stream has no data
// right away.
sleep(5);
// turning blocking off allows stream to keep moving eventhough no data is coming
// through it. Required to make the time out work correctly.
socket_set_blocking($stream,false); // the time out is set less than the php time out. Otherwise you run the risk of the
// php timing out if the stream hangs.
stream_set_timeout($stream,25);
$start = mktime();
// manual indicator to exit the while
$exit = false;
while ($exit == false) {
$read .= fread($stream,5120);
// this pulls meta data from the stram so we can test for the time out.
$info = stream_get_meta_data($stream);
// test for the echo'ed __end_cmd_response__
if (ereg("__end_cmd_response__",$read)) {
$exit = true;
break;
// check to see if the stream has timed out.
} elseif ($info["timed_out"]) {
// i just add this to tell the user that the stream timed out.
$read .= "<i>Exited stream reading (25 seconds). Check changes online.</i>\n";
$exit = true;
break;
}
}
fclose($stream);
return $read;
}
?>
Oscar Gosdinski wrote:
Hello everybody:
I need my PHP application connects through SSH to other server for getting some info to display and i tried the following code to test lib ssh2:
$server = ...; $user = ...; $passwd = ...;
$con = ssh2_connect($server, 22); if (ssh2_auth_password($con, $user, $passwd)) { $s = ssh2_exec($con, 'ls -l'); $file = fopen("/tmp/test", "w"); while ($line = stream_get_line($s, 1024)) { fwrite($file, $line); fflush($file); } fclose($file); } else { echo "Authentication Error"; }
The file /tmp/test was created but it does not have any data. I can connect to the server using a normal ssh session in my laptop and the 'ls -l' command returns me information. I think that i installed correctly the ssh2 functions because if a put an incorrect password i get the "Authentication Error" message.
I searched the web for info about similar problems and no results. Please, anyone can help me with this problem because i have been spending three days with this.
Thanks in advance for your answers.
-- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php