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
PHP function ssh2_connect is not working
提问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
回答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
回答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

