'PHP function ssh2_connect is not working

Following is my script:

    <?php
    $connection = ssh2_connect('XX.XX.XX.XX', 22);
    ssh2_auth_password($connection, 'root', '******');

    $stream = ssh2_exec($connection, 'useradd -d /home/users/test -m testftp');
    $stream = ssh2_exec($connection, 'passwd testftp');
    $stream = ssh2_exec($connection, 'password');
    $stream = ssh2_exec($connection, 'password');
    ?>

It showing the following error:

Fatal error: Call to undefined function ssh2_connect() in /home/chaosnz/public_html/fotosnap.net/test.php on line 2

How can I deal with this?

Thanks



Solution 1:[1]

Honestly, I'd recommend using phpseclib, a pure PHP SSH2 implementation. Example:

<?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');
?>

It's a ton more portable, easier to use and more feature packed too.

Solution 2:[2]

I have solved this on ubuntu 16.4 PHP 7.0.27-0+deb9u and nginx

sudo apt install php-ssh2 

Solution 3:[3]

You need to install ssh2 lib

sudo apt-get install libssh2-php && sudo /etc/init.d/apache2 restart

that should be enough to get you on the road

Solution 4:[4]

If you are running a bomebrew on OSX, I used the following to install it:

brew install php56-ssh2

That worked for me. I pulled it from here. There should also be Ubuntu and OSX using mac port as well.

Solution 5:[5]

I am running CentOS 5.6 as my development environment and the following worked for me.

su -
pecl install ssh2
echo "extension=ssh2.so" > /etc/php.d/ssh2.ini

/etc/init.d/httpd restart

Solution 6:[6]

To expand on @neubert answer, if you are using Laravel 5 or similar, you can use phpseclib much simpler like this:

Run composer require phpseclib/phpseclib ~2.0

In your controller add

use phpseclib\Net\SSH2;

Then use it in a controller method like:

 $host = config('ssh.host');
 $username = config('ssh.username');
 $password = config('ssh.password'); 
 $command = 'php version';

 $ssh = new SSH2($host);
    if (!$ssh->login($username, $password)) {
        $output ='Login Failed';
    }
    else{
        $output = $ssh->exec($command);
 }

Solution 7:[7]

For today pecl install ssh2 requires old versions of php (<=6.0). Here https://pecl.php.net/package/ssh2 you can see the latest beta versions of ssh2 supporting php7 and php8. So you should install one of them:

pecl install ssh2-1.3.1

* Do not forget to enable this ssh2.so extension in your php.ini file.

Solution 8:[8]

For WHM Panel

Menu > Server Configuration > Terminal:

yum install libssh2-devel -y

Menu > Software > Module Installers

  1. PHP PECL Manage Click
  2. ssh2 Install Now Click

Menu > Restart Services > HTTP Server (Apache)

Are you sure you wish to restart this service?

Yes

ssh2_connect() Work!

Solution 9:[9]

I know there are answers but I am just simplifying the answer here.

I have same issue and I fixed it with below solution in ubuntu 20:

sudo apt-get install libssh2-1 php7.1-ssh2 -y

you can change the php version as per your need like: php7.4-ssh2

Ref: https://blog.programster.org/ubuntu-16-04-install-php-ssh2-extension

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 neubert
Solution 2 Nanhe Kumar
Solution 3 zppinto
Solution 4 Community
Solution 5 crmpicco
Solution 6 Wesley Smith
Solution 7 SpinyMan
Solution 8 Limitless isa
Solution 9 Hemant Kumar