php.net |  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
Votes:1
Avg. Score:5.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:1 (100.0%)
From: langemeijer@php.net Assigned:
Status: Open Package: ssh2 (PECL)
PHP Version: Irrelevant OS: any
Private report: No CVE-ID: None
 [2012-10-07 19:29 UTC] langemeijer@php.net
Description:
------------
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');
...
$Channel->execute(....);

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.



Patches

Pull Requests

History

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?:

<?php
include('Net/SSH2.php');

$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] langemeijer@php.net
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: Thu Oct 31 22:01:27 2024 UTC