robetus Posted April 6, 2023 Share Posted April 6, 2023 (edited) 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 April 6, 2023 by robetus updated reason Quote Link to comment https://forums.phpfreaks.com/topic/316089-php-81-upgrade-and-error-in-ssh2-output-stream/ Share on other sites More sharing options...
requinix Posted April 6, 2023 Share Posted April 6, 2023 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? Quote Link to comment https://forums.phpfreaks.com/topic/316089-php-81-upgrade-and-error-in-ssh2-output-stream/#findComment-1607068 Share on other sites More sharing options...
robetus Posted April 6, 2023 Author Share Posted April 6, 2023 (edited) 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. Edited April 6, 2023 by robetus Quote Link to comment https://forums.phpfreaks.com/topic/316089-php-81-upgrade-and-error-in-ssh2-output-stream/#findComment-1607069 Share on other sites More sharing options...
robetus Posted April 6, 2023 Author Share Posted April 6, 2023 (edited) 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 April 6, 2023 by robetus Quote Link to comment https://forums.phpfreaks.com/topic/316089-php-81-upgrade-and-error-in-ssh2-output-stream/#findComment-1607070 Share on other sites More sharing options...
requinix Posted April 6, 2023 Share Posted April 6, 2023 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? Quote Link to comment https://forums.phpfreaks.com/topic/316089-php-81-upgrade-and-error-in-ssh2-output-stream/#findComment-1607071 Share on other sites More sharing options...
robetus Posted April 6, 2023 Author Share Posted April 6, 2023 You're right in php 8.1 ssh2_exec is not functioning properly. I need to figure out why. Quote Link to comment https://forums.phpfreaks.com/topic/316089-php-81-upgrade-and-error-in-ssh2-output-stream/#findComment-1607072 Share on other sites More sharing options...
requinix Posted April 6, 2023 Share Posted April 6, 2023 Quote Link to comment https://forums.phpfreaks.com/topic/316089-php-81-upgrade-and-error-in-ssh2-output-stream/#findComment-1607074 Share on other sites More sharing options...
robetus Posted April 6, 2023 Author Share Posted April 6, 2023 (edited) 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 April 6, 2023 by robetus Quote Link to comment https://forums.phpfreaks.com/topic/316089-php-81-upgrade-and-error-in-ssh2-output-stream/#findComment-1607078 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.