46

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

Shiplu Mokaddim
  • 56,364
  • 17
  • 141
  • 187
Ravi Soni
  • 2,210
  • 3
  • 31
  • 53
  • 8
    What have you tried? What hasn't worked? What research have you done to solve this problem? Have you made sure that [the SSH2 PECL extension](http://php.net/book.ssh2) is installed and enabled? It is *not* a part of standard PHP. – Charles Dec 27 '12 at 06:14
  • 1
    i have installed the SSH2 PECL extension and it working fine thanks all for you help.. – Ravi Soni Dec 27 '12 at 06:39
  • 2
    When you can, you should post that as your answer and mark it accepted. You might need to wait a bit before the system will let you do it. This action will help future users with the same problem. – Charles Dec 27 '12 at 06:40
  • Any reason for down voted :( – Ravi Soni Dec 27 '12 at 06:41
  • 2
    While I was not one of the downvoters, I encourage you to hover over the downarrow and read the description given. This answer doesn't really show a lot of research effort, so that's probably why others decided to downvote you. – Charles Dec 27 '12 at 06:43
  • Possible duplicate of [Execute a shell command through ssh using PHP](http://stackoverflow.com/questions/18373632/execute-a-shell-command-through-ssh-using-php) – Maurizio Brioschi Jan 10 '17 at 15:23
  • Try to see the answers on this post http://stackoverflow.com/questions/18373632/execute-a-shell-command-through-ssh-using-php/41572063#41572063 – Maurizio Brioschi Jan 10 '17 at 15:25

10 Answers10

30

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.

neubert
  • 15,947
  • 24
  • 120
  • 212
  • 3
    Could be a good solution for some people but I need something to work without any additional installations. – Julio Bailon May 19 '16 at 00:14
  • 1
    libssh2 requires libssh2 be installed. phpseclib just requires the files be somewhere on the filesystem but if you want to consider that to be an installation... well, then, I guess the only real alternative left to you is to write your own SSH implementation. good luck with that. – neubert May 19 '16 at 14:01
  • 7
    Why tell someone to use a different lib when they are asking how to make the one they're using work? – j_allen_morris Aug 03 '19 at 20:21
  • 2
    @j_allen_morris - if someone is wanting to get `ereg` working in their project would you tell them to write a shim or would you tell them to use replace their `ereg` function call with the more modern `preg_match`? I'd say both "perspectives" are valid. Also, keep in mind that 23 people other than myself thought that this was a good / relevant enough answer to justify an up vote (as of this moment this answer has 23 up votes and 3 down votes). Certainly you're free to disagree. – neubert Aug 03 '19 at 23:42
  • 1
    @neubert "write your own SSH implementation" really? PHP has built-in functions to make ssh or sftp connection. – le hien Oct 06 '20 at 10:08
  • 1
    PHPSECLIB is underrated! Thanks!! – GTodorov Feb 26 '23 at 17:13
29

I have installed the SSH2 PECL extension and its working fine thanks all for you help...

Abhishek Gurjar
  • 7,426
  • 10
  • 37
  • 45
Ravi Soni
  • 2,210
  • 3
  • 31
  • 53
18

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

sudo apt install php-ssh2 
Nanhe Kumar
  • 15,498
  • 5
  • 79
  • 71
  • 1
    This solved the error `Call to undefined function ssh2_connect()` on Ubuntu 20.04LTS with PHP7.4 as well – CodeNinja Nov 30 '22 at 10:46
13

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

zppinto
  • 287
  • 7
  • 20
Daniel Acevedo
  • 131
  • 1
  • 2
8

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.

Community
  • 1
  • 1
Boni
  • 127
  • 1
  • 4
  • 2
    After installed it, I still get `Call to undefined function App\ssh2_connect()` - Do you know why ? – code-8 Jan 08 '16 at 20:58
3

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
crmpicco
  • 16,605
  • 26
  • 134
  • 210
2

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);
 }
Wesley Smith
  • 19,401
  • 22
  • 85
  • 133
2

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.

SpinyMan
  • 436
  • 5
  • 9
2

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

Hemant Kumar
  • 1,025
  • 15
  • 32
1

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!

Limitless isa
  • 3,689
  • 36
  • 28