If I run this script on my workstation (Ubuntu) it results in a directory listing. If I run it on the server (a Red Hat box where it actually belongs) it retrieves an empty string as the result. I cannot for the life of me isolate any reason for this. Any thoughts on why the server can connect, authenticate, and execute the command but get an empty stream?

The script:

require_once "db_bootstrap.php";
require_once MY_PATH . "/includes/classes/SSHConnection.php";
function fnignore($msg) {
        echo "fnignore: " . $msg . "\n";
}
function fndebug($msg, $language, $always_display) {
        echo "fndebug: " . $msg . "\n";
        echo "\t" . $language . "\n";
        echo "\t" . $always_display . "\n";
}
function fnmacerror($pkt) {
        echo "fnmacerror:\n";
        var_dump($pkt);
        echo "=== end ===";
}
function fndisconnect($reason, $msg, $language) {
        echo "fndisconnect: " . $reason . "\n";
        echo "\t" . $msg . "\n";
        echo "\t" . $language . "\n";
}

$connection = ssh2_connect(SSHConnection::SSH_DB1_HOST, 22, NULL, array(
        "ignore" => "fnignore",
        "debug" => "fndebug",
        "macerror" => "fnmacerror",
        "disconnect" => "fndisconnect"
));
echo "connection:\n";
var_dump($connection);

$auth_result = ssh2_auth_password($connection, SSHConnection::SSH_DB1_USERNAME, SSHConnection::SSH_DB1_PASSWD);
echo "auth_result:\n";
var_dump($auth_result);

$stream = ssh2_exec($connection, "ls -al");
echo "stream:\n";
var_dump($stream);

$output = stream_get_contents($stream, 1000000, 0);
echo "output:\n";
var_dump($output);

The result on the server:

connection:
resource(21) of type (SSH2 Session)
auth_result:
bool(true)
stream:
resource(22) of type (stream)
output:
string(0) ""

GRRRR!!

On my workstation, this is the output:

connection:
resource(21) of type (SSH2 Session)
auth_result:
bool(true)
stream:
resource(22) of type (stream)
output:
string(525) "total 32
drwx------  2 imagedaemon imagedaemon 4096 Jul  9 20:53 .
drwxr-xr-x. 7 root        root        4096 Jul  9 20:52 ..
-rw-------  1 imagedaemon imagedaemon  307 Jul 10 21:22 .bash_history
-rw-r--r--  1 imagedaemon imagedaemon   18 Apr 23  2012 .bash_logout
-rw-r--r--  1 imagedaemon imagedaemon  176 Apr 23  2012 .bash_profile
-rw-r--r--  1 imagedaemon imagedaemon  124 Apr 23  2012 .bashrc
-rw-r--r--  1 imagedaemon imagedaemon  500 Feb 27  2012 .emacs
-rw-r--r--  1 imagedaemon imagedaemon  121 Oct  8  2012 .kshrc
"

wtf??

    Have you read the user notes on [man]stream_get_contents/man?

    Any possibility the stream is blocking, waiting for something?

    Just a though ... hope it's helpful. 🙂

      Thanks dalecosp! You deserve a yaller jeep. I added a stream_set_blocking command and that appears to have solved the problem (see below).

      HOWEVER, I that introduces worry about another sort of problem that you were also helping with. I'm still a bit worried about that one.

      Here's the modified script that seems to work in both places. The ironic bit is that the machine where it was returning an empty string before is on the same LAN as the machine being connected to. My workstation on the other hand is 2000 miles from that server.

      require_once "db_bootstrap.php";
      require_once MYPATH . "/erp_php/includes/classes/SSHConnection.php";
      function fnignore($msg) {
              echo "fnignore: " . $msg . "\n";
      }
      function fndebug($msg, $language, $always_display) {
              echo "fndebug: " . $msg . "\n";
              echo "\t" . $language . "\n";
              echo "\t" . $always_display . "\n";
      }
      function fnmacerror($pkt) {
              echo "fnmacerror:\n";
              var_dump($pkt);
              echo "=== end ===";
      }
      function fndisconnect($reason, $msg, $language) {
              echo "fndisconnect: " . $reason . "\n";
              echo "\t" . $msg . "\n";
              echo "\t" . $language . "\n";
      }
      
      $connection = ssh2_connect(SSHConnection::SSH_DB1_HOST, 22, NULL, array(
              "ignore" => "fnignore",
              "debug" => "fndebug",
              "macerror" => "fnmacerror",
              "disconnect" => "fndisconnect"
      ));
      echo "connection:\n";
      var_dump($connection);
      
      $auth_result = ssh2_auth_password($connection, SSHConnection::SSH_DB1_USERNAME, SSHConnection::SSH_DB1_PASSWD);
      echo "auth_result:\n";
      var_dump($auth_result);
      
      $stream = ssh2_exec($connection, "ls -al");
      echo "stream:\n";
      var_dump($stream);
      
      // added this
      $blocking = stream_set_blocking($stream, 1);
      echo "blocking:\n";
      var_dump($blocking);
      
      
      $output = stream_get_contents($stream, 1000000, 0);
      echo "output:\n";
      var_dump($output);
      
      
        Write a Reply...