【问题标题】:PHP SSH2 SFTP Using Username, Password & Key Pair使用用户名、密码和密钥对的 PHP SSH2 SFTP
【发布时间】:2013-09-05 18:03:00
【问题描述】:

数据供应商已请求一个公共 SSH 密钥以使用其 SFTP 服务器。他们为我们提供了:

  • 用户名
  • 密码
  • 主机名
  • 端口

我正在尝试使用私钥和用户名/密码在 php 中使用流包装器 ssh2.sftp//。我的代码如下:

$options = array(
    'ssh2' => array(
        'privkey_file' => '/path/to/priv_key',
    ),
);
$context = stream_context_create($options);
fopen('ssh2.sftp://<username>:<password>@<host>:<port>', 'rb', false, $context);

结果:

Warning: fopen(ssh2.sftp://...@<ip>...): failed to open stream: operation failed in ...

我可以使用 linux 的 sftp 命令进行连接,因此我知道连接正常,并且凭据工作正常。

sftp -oPort=<port> -i /path/to/priv_key -v <username>@<host>

结果:

Authenticated with partial success.
password: <password-entered>
debug1: Authentication succeeded (password).
debug1: channel 0: new [client-session]
debug1: Sending subsystem: sftp
sftp>

有人有什么想法吗?

我对使用ssh2 函数的想法持开放态度,但我强烈希望使用file systemstream 函数。

【问题讨论】:

标签: php ssh stream sftp


【解决方案1】:

老实说,我认为您最好使用phpseclib, a pure PHP SFTP implementation。除此之外,它比使用 libssh2 更容易诊断问题。

另外,如果你想使用文件系统和流函数,phpseclib 也支持这些:

https://github.com/phpseclib/phpseclib/blob/master/phpseclib/Net/SFTP/Stream.php

【讨论】:

    最近更新 更多