PHP function ssh2_connect is not working
Asked Answered
E

11

47

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

Extend answered 27/12, 2012 at 6:13 Comment(7)
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 is installed and enabled? It is not a part of standard PHP.Forestry
i have installed the SSH2 PECL extension and it working fine thanks all for you help..Extend
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.Forestry
Any reason for down voted :(Extend
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.Forestry
Possible duplicate of Execute a shell command through ssh using PHPSeismo
Try to see the answers on this post #18374132Seismo
E
29

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

Extend answered 27/12, 2012 at 6:41 Comment(2)
How did you install that ?Demogorgon
with this programster.blogspot.ch/2013/06/…Arsenal
D
31

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.

Diapedesis answered 2/1, 2013 at 21:1 Comment(6)
Could be a good solution for some people but I need something to work without any additional installations.Lamppost
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.Diapedesis
Why tell someone to use a different lib when they are asking how to make the one they're using work?Switcheroo
@Switcheroo - 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.Diapedesis
@Diapedesis "write your own SSH implementation" really? PHP has built-in functions to make ssh or sftp connection.Acrolein
PHPSECLIB is underrated! Thanks!!Newby
E
29

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

Extend answered 27/12, 2012 at 6:41 Comment(2)
How did you install that ?Demogorgon
with this programster.blogspot.ch/2013/06/…Arsenal
D
18

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

sudo apt install php-ssh2 
Deva answered 9/4, 2018 at 13:27 Comment(1)
This solved the error Call to undefined function ssh2_connect() on Ubuntu 20.04LTS with PHP7.4 as wellFallow
P
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

Pyo answered 6/5, 2015 at 13:41 Comment(2)
Package libssh2-php is not available, but is referred to by another package. How to solve this?Somniloquy
By usign the alternative suggested, sudo apt-get install php-ssh2Eighth
A
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.

Ancillary answered 7/10, 2015 at 17:5 Comment(1)
After installed it, I still get Call to undefined function App\ssh2_connect() - Do you know why ?Demogorgon
Z
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
Zounds answered 31/10, 2013 at 13:37 Comment(0)
I
3

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);
 }
Ilarrold answered 26/4, 2017 at 3:57 Comment(0)
B
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.

Boldt answered 6/10, 2021 at 14:39 Comment(0)
C
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

Charters answered 25/2, 2022 at 12:15 Comment(0)
B
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!

Baseboard answered 30/11, 2020 at 9:3 Comment(0)
B
0

I feel like this 11 year old discussion deserves an update. I switched from the PHPSecLib library to the built-in SSH2 library for PHP 7 & 8 because it seems to connect faster and is more standard. It also has the ability to use key passphrases now, so there is no need to use 3rd party libraries anymore.

Boris answered 14/4 at 14:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.