php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #57140 ssh2_exec bug
Submitted: 2006-07-13 04:42 UTC Modified: 2006-07-13 10:32 UTC
From: robert dot reichel at zg dot t-com dot hr Assigned:
Status: Not a bug Package: ssh2 (PECL)
PHP Version: 5.1.0 OS: Windows XP
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: robert dot reichel at zg dot t-com dot hr
New email:
PHP Version: OS:

 

 [2006-07-13 04:42 UTC] robert dot reichel at zg dot t-com dot hr
Description:
------------
When I execute command on remote server with 'ssh2_exec' function and executed command is valid, I receive expected result. For example if I execute command 'dir', I receive stream with all directory entries.

But if I execute an invalid command, for example 'dirr', I receive empty stream. When I execute same command using my SSH client (I use PuTTY), I receive the following message: '-bash: dirr: command not found'

I also tried to use callback functions but without success.



Reproduce code:
---------------
$Hostname = 'myhost';
$Username = 'myuser';
$Password = 'mypass';
$Output = null;
$Methods = array();
$Callbacks = array ('ignore'=>'cbIgnore', 'debug'=>'cbDebug', 'macerror'=>'cbMACError', 'disconnect'=>'cbDisconnect');

//connecting remote server...
if (false === $Connection = ssh2_connect($Hostname, 22, $Methods, $Callbacks)) {
  die ("Unable to connect '$Hostname'.\n");
} else {
  echo "Done.\n";
}
//authenticating user...
if (false === ssh2_auth_password($Connection, $Username, $Password)) {
  die ("Unable to authenticate user.\n");
}
//executing command on remote server
if (false === $Stream = ssh2_exec($Connection, 'di')) {
  die ("Unable to execute command on remote server.\n");
}
//set blocking mode on a stream
stream_set_blocking($Stream, true);

//set output string
while ($Block = fread($Stream, 8192)) {
  $Output.= $Block;
}
fclose ($Stream);
echo $Output;

// CALLBACK FUNCTIONS
function cbIgnore ($message)
{
  die ("IGNORE: $message");
}
  
function cbDebug ($message, $language, $always_display)
{
  die ("DEBUG: $message");
}
  
function cbMACError ($packet)
{
  echo ("MAC_ERROR: $packet");
  return true;
}
  
function cbDisconnect ($reason, $message, $language)
{
  die ("DISCONNECT: $message");
}


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2006-07-13 10:32 UTC] pollita@php.net
That's because errors come back on the STDERR substream.  You can fetch this substream using:

$stdio = ssh2_exec($connection, "dirr");
$stderr = ssh2_fetch_stream($stdio, SSH2_STREAM_STDERR);
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Fri May 09 13:01:28 2025 UTC