PHP 函数 ssh2_connect 不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14050231/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 06:31:23  来源:igfitidea点击:

PHP function ssh2_connect is not working

phpssh

提问by ravisoni

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

谢谢

采纳答案by ravisoni

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

我已经安装了 SSH2 PECL 扩展并且它工作正常,谢谢大家的帮助...

回答by neubert

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

老实说,我建议使用 phpseclib,一个纯 PHP SSH2 实现。例子:

<?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.

它更便携、更易于使用且功能更丰富。

回答by Daniel Acevedo

You need to install ssh2 lib

您需要安装ssh2 库

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

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

that should be enough to get you on the road

这应该足以让你上路

回答by Nanhe Kumar

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

我已经在 ubuntu 16.4 PHP 7.0.27-0+deb9u 和 nginx 上解决了这个问题

sudo apt install php-ssh2 

回答by Boni

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

如果您在 OSX 上运行 bomebrew,我使用以下方法安装它:

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.

那对我有用。我从这里拉出来的。也应该有使用 mac 端口的 Ubuntu 和 OSX。

回答by Wesley Smith

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

要扩展@neubert 答案,如果您使用的是 Laravel 5 或类似版本,则可以使用更简单的 phpseclib,如下所示:

Run composer require phpseclib/phpseclib ~2.0

composer require phpseclib/phpseclib ~2.0

In your controller add

在您的控制器中添加

use phpseclib\Net\SSH2;

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);
 }

回答by crmpicco

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

我正在运行 CentOS 5.6 作为我的开发环境,以下内容对我有用。

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

/etc/init.d/httpd restart