【问题标题】:Use ~/.ssh/config when opening streams with PHP SSH2使用 PHP SSH2 打开流时使用 ~/.ssh/config
【发布时间】:2012-10-08 13:13:54
【问题描述】:

在 PHP 中处理 SSH2 流时是否可以使用用户的 SSH 客户端配置?在大多数系统上,它存储在 ~/.ssh/config 中。

就我而言,我在 SSH 配置中配置了多个别名。但是,当我尝试在我的 PHP 代码中使用这些别名时,它们不起作用。

示例 ~/.ssh/config

Host foo
    HostName my.server
    Port 22
    User sander
    IdentityFile ~/.ssh/id_rsa

失败但我想开始工作的示例 PHP 代码:

<?php
$text = file_get_contents('ssh2.sftp://foo/bar.txt');

【问题讨论】:

  • 组权限会变得很荒谬

标签: php ssh stream


【解决方案1】:

是的,这是完全可能的。该文件的文件格式is specified。您需要做的就是为它编写一个解析器(也许这已经写过了,至少类似的东西),然后使用您的主机名(这里:foo)来访问该配置。

然后您使用这些设置进行连接。

如果你喜欢完全符合你的例子:

$text = file_get_contents('ssh2.sftp://foo/bar.txt');

这更复杂,我认为甚至不可能。但是,如果您更改前缀,您应该能够将您的实现注入为 a stream wrapper you register,它“包裹”在 ssh2 包装器周围,因此它的使用同样简单:

$text = file_get_contents('my.ssh2.sftp://foo/bar.txt');

【讨论】:

  • 这是个好主意。不幸的是,我正在处理一个使用 file_get_contents() 的外部库,因此在其中添加自定义解析器并不容易。
  • 这就是流包装器的用途。
【解决方案2】:
$userName =username;

$password = password;

$domain = domain;

$connection = ssh2_connect($domain, 22);


ssh2_auth_password($connection, $userName, $password);
$sftp =ssh2_sftp($connection); 
$content = file_get_contents("ssh2.sftp://{$sftp}{$sourceFile}");
$data = $content ."new content";

$content = file_put_contents("ssh2.sftp://{$sftp}{$sourceFile}",$data);

【讨论】:

    【解决方案3】:

    如果您在http://pecl.php.net/package/ssh2 查看 ssh2 包装器的源代码,它们似乎根本不支持别名。

    发布到他们的邮件列表中,可能很容易添加别名支持。

    【讨论】:

      【解决方案4】:

      对于

      $sftpConnection = ssh2_connect($host);
      $sftp = ssh2_sftp($sftpConnection);
      $fp = fopen("ssh2.sftp://" . intval($sftp) . $remoteFile, "r");
      

      https://bugs.php.net/bug.php?id=73597

      【讨论】:

        猜你喜欢
        • 2017-12-02
        • 2010-12-05
        • 1970-01-01
        • 1970-01-01
        • 2012-07-13
        • 1970-01-01
        • 2014-04-06
        • 1970-01-01
        • 2020-01-27
        相关资源
        最近更新 更多