|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #63233 Easier way to do ssh exec()
Submitted: 2012-10-07 19:29 UTC Modified: 2012-10-22 19:02 UTC
Avg. Score:5.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:1 (100.0%)
From: Assigned:
Status: Open Package: ssh2 (PECL)
PHP Version: Irrelevant OS: any
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
Block user comment
Status: Assign to:
Bug Type:
New email:
PHP Version: OS:


 [2012-10-07 19:29 UTC]
Please consider this:

$Channel = ssh2_command_channel ( resource $session [, string $term_type = 
"vanilla" [, array $env [, int$width = 80 [, int $height = 25 [, int 
$width_height_type = SSH2_TERM_UNIT_CHARS ]]]]] );

$retval = $Channel->execute ('/usr/bin/fsck', &$stdout, &$stderr, $stdin);
$Channel->setenv('key', 'value');

unset($Channel); // Closes the channel.

This way executing a command is easy:
- A blocking execute call,
- No reading of or writing to streams, 
- Able to get the return value of an executable in an easy way

It would not be ideal if you have big amounts of data flowing in stdin/stdout, 
but for a lot of use-cases this would be much easier.


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2012-10-08 15:35 UTC] zelnaga at gmail dot com
Correct me if I'm wrong but does this do what you're wanting?:


$ssh = new Net_SSH2('www.domain.tld');
if (!$ssh->login('username', 'password')) {
    exit('Login Failed');

echo $ssh->exec('pwd');
echo $ssh->exec('ls -la');

(using phpseclib's Net_SSH2)
 [2012-10-22 17:30 UTC] william dot leonard at lxcenter dot org
zelnaga - Is that library you are referencing the same as the one for this bug?

Currently ssh2 (PECL) closes the channel after each ssh2_exec or ssh2_shell.  So with this current way, things that you had previously done aren't kept.  If you were to run a command get the results with a shell variable and then the next command uses that variable, the variable would be lost as an example.  This enhancement reuses the same channel for each command.

In your example I would change the exec to: 
echo $ssh->exec('cd ./newdirectory');
echo $ssh->exec('ls -la');

ls -la currently would show the directory above newdirectory.  This enhancement would allow ls -la to show ./newdirectory.

The current workaround for this would be:
echo $ssh->exec('cd ./newdirectory && ls -la');
 [2012-10-22 19:02 UTC]
Please guys. This is not a support forum. It's a bug and feature request 
database. In this case I, developer for this extension, am making a feature 
request bug to formalize future development.
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Mon Jul 15 00:01:28 2024 UTC