Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SSH::status() always returns false #46

Open
axis80 opened this issue Jun 29, 2017 · 2 comments
Open

SSH::status() always returns false #46

axis80 opened this issue Jun 29, 2017 · 2 comments

Comments

@axis80
Copy link

axis80 commented Jun 29, 2017

If I'm correctly understanding the syntax for retrieving the exit status of the last command executed, then it doesn't seem to work. Here is code to reproduce the issue:

$command = 'true'; // It doesn't matter what command you put here
SSH::run($command);
$status = SSH::status();
var_dump($status);  // use var_dump instead of dd - it's clearer
exit;

Result:

bool(false)

As noted above, it doesn't matter what command you send it. It always says bool(false).

I dug through the code, and what appears to be happening is that the SSH connection is dropped as soon as the specified commands are done running. When execution of the above code makes it down to SSH::status(), it ends up opening a new connection and returning the status of that.

I also tried placing the SSH::status() inside a closure, like this:

SSH::run($command, function($line)
{
  $status = SSH::status();
  var_dump( $status );
  exit;
});

Still no love. Although I didn't debug this one as thoroughly, I suspect what is happening here again is that the call to phpseclib->exec() is finishing and closing the connection before the status can be retrieved.

Does that assessment make sense, or am I just calling status() incorrectly?

@stefandanaita
Copy link

stefandanaita commented Jun 30, 2017

Do it like this:

$remote = \App::make('remote');
$connection = $remote->connection('default');

$connection->run('command', function($line) {
    echo $line;
});

if($connection->status() != 0) {
    // failed
}

@stidges
Copy link

stidges commented Aug 31, 2017

Every time you make a call to the SSH facade a new connection is setup. If you keep a reference to the connection the status will be returned properly:

$ssh = SSH::connection(); // or SSH::into('env')
$ssh->run('...');
$status = $ssh->status(); // $status will now hold the exit status

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants