【问题标题】:SSH Connection to a remote server using PHP使用 PHP SSH 连接到远程服务器
【发布时间】:2018-08-06 17:41:07
【问题描述】:

我想使用 php 与远程服务器建立 SSH 连接。

我在 Linux/CEntOS 上使用 php 5.3。

到目前为止我做了什么:

$connection = ssh2_connect('192.168.1.22', 22);
ssh2_auth_password($connection, '_username', '_password');
$stream = ssh2_exec($connection, 'ls -l');

但是我收到了这个错误:

致命错误:调用未定义函数 ssh2_connect()

所以,我的问题是:

  1. 难道ssh2_*函数没有默认安装在php中?

  2. 是否需要扩展或库才能使用这些功能?

  3. 我该如何解决这个问题?

【问题讨论】:

标签: php ssh


【解决方案1】:

您可以配置您的服务器(Ubuntu):

sudo apt-get install libssh2-php
sudo service apache2 restart

【讨论】:

  • 在 ubuntu 16.04 中要安装的包是 php-ssh2
【解决方案2】:

phpseclib

  • herehere(直接链接)下载,

  • 将其包含到项目中,

然后:

   include('Net/SSH2.php');
   $ssh = new Net_SSH2('www.example.com');
   $ssh->login('username', 'password') or die("Login failed");
   echo $ssh->exec('command');

【讨论】:

  • 假设你想远程执行一个'ls'命令并将结果带到PHP中......你是怎么做的?
【解决方案3】:

ssh_* 函数并非默认安装在 php 核心中。 您必须使用 pecl 添加它们,有关详细说明,请参阅 php 手册:http://de1.php.net/manual/en/ssh2.installation.php

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-06-17
    • 2017-09-27
    • 2012-11-26
    • 2015-10-09
    • 2010-12-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多