Jump to content

Recommended Posts

This is a slim downed version of what I'm facing:

$connection = ssh2_connection(71.45.71.163, ubu22, passw0rd, 22);
$stream = ssh2_exec($connection, 'echo "Hello world"');
$errorStream = ssh2_fetch_stream($stream, SSH2_STREAM_STDERR);
// Enable blocking for both streams
stream_set_blocking($errorStream, true);
stream_set_blocking($stream, true);
$output = stream_get_contents($stream);
$error = stream_get_contents($errorStream);

$stream is giving a boolean value and in php 8.1 you cannot output a boolean value with ssh2_fetch_stream, it needs to be a resource. I need to figure out how to make it a resource and not a boolean. The exact error:

TypeError: ssh2_fetch_stream(): Argument #1 ($channel) must be of type resource, bool given

How can I make the $stream value a resource type instead of a boolean value? All php 7.x versions are okay with the bool but php 8.1+ is not.

Edited by robetus
updated reason
24 minutes ago, robetus said:

How can I make the $stream value a resource type instead of a boolean value?

Wrong question. What you should be asking is "why is $stream a boolean and not a resource?"

It is because ssh2_exec returned a boolean - false, specifically. So why is it returning false?

The documentation says it will return false "on failure". Not especially helpful, but at the very least it means there was a failure in trying to run your command.
Do you have any idea what it might be? Any messages in your error log? Anything changed besides literally the fact that you are using PHP 8 instead of 7?

if(gettype($stream) == "resource") { }

fixes it but then I obviously won't get an stream error output. Found this little tidbit online:

PHP Resource
The special resource type is not an actual data type. It is the storing of a reference to functions and resources external to PHP.

A common example of using the resource data type is a database call.

I need to store the functions and then output them in the $errorStream variable. Still learning here, but it's fun.

Edited by robetus
25 minutes ago, robetus said:

Wouldn't the same be if it returned true? It doesn't want a bool value at all I think, true or false. The only error I have is what I stated unfortunately.

It would be the same error message if it returned true, that's right. But the function does not return true. It only ever returns a resource or false.

 

There is a problem and neither of us knows exactly what it is just yet. This unknown problem is causing ssh2_exec to return false. If you can find out what the problem is and fix it, then your code will get the resource it expects and everything should work.
To try to find out what the problem is, my question was: is there anything else that has changed with your system, or perhaps the system you are trying to SSH into, besides the fact that you are using PHP 8 instead of PHP 7? Any other configuration changes? Could it be that you are using PHP 8 not because the server upgraded but because you're using a whole new server entirely?

Okay I got it, so in php 8.1 the stream output cannot be empty. If it's empty or if it's a simple true false boolean it will throw an error, so what I did to get around it is quite simple:

$connection = ssh2_connection(71.45.71.163, ubu22, passw0rd, 22);
$stream = ssh2_exec($connection, 'echo "Hello world"');
if (!empty($stream)) {  //if empty stop here
$errorStream = ssh2_fetch_stream($stream, SSH2_STREAM_STDERR);
// Enable blocking for both streams
stream_set_blocking($errorStream, true);
stream_set_blocking($stream, true);
$output = stream_get_contents($stream);
$error = stream_get_contents($errorStream);
}

Yep, and that was it. It only showed an error if $stream was empty. If it was empty it does not do any of the stream output execution. If there is no error there is no need to attempt to output any error, because if there is no error nothing is outputted anyway. If there  Easy peasy.

Edited by robetus
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.