php / pecl-networking-ssh2

Bindings for the libssh2 library
http://pecl.php.net/package/ssh2
Other
51 stars 59 forks source link

ssh2_exec: retrieve the command exit code #73

Closed mlocati closed 1 year ago

mlocati commented 1 year ago

What about adding a new function to retrieve the exit code if a command executed with ssh2_exec()?

I'm thinking about a new ssh2_get_exit_status() function that could be used like this:

$connection = ssh2_connect($host);
ssh2_auth_password($connection, $username, $password);
$channel = ssh2_exec($connection, $command);
$exitCode = ssh2_get_exit_status($channel);
fclose($channel);

And internally, ssh2_exec() could use libssh2_channel_get_exit_status().

mlocati commented 1 year ago

Never mind, I checked the source code and I saw that since version 0.13 this can be already done:

$connection = ssh2_connect($host);
ssh2_auth_password($connection, $username, $password);
$channel = ssh2_exec($connection, $command);
$metadata = stream_get_meta_data($channel);
$exitCode = $metadata['exit_status'];
fclose($channel);