【问题标题】:SSH2 Not Working On New ServerSSH2 在新服务器上不起作用
【发布时间】:2016-12-15 17:08:11
【问题描述】:

我们更改了托管公司,而 ssh2 所在的位置不再有效。我们在新服务器上启用了ssh2,同时启用了allow_url_fopen

我能看到的唯一区别是旧服务器使用PHP 5.4.45,而新服务器使用PHP 5.6.28

但是,我现在收到以下错误。 Could not open remote file: ssh2.sftp://Resource id #337/my/directory/file.txt

这是我的代码示例:

$remote_host = "myhostinfohere";
$remote_port = 22;
$remote_user = "myuser";
$remote_pass = "mypassword";
$remote_dir = "/my/directory/";
$remote_file = 'file.txt';

try {
    $remote_conn = ssh2_connect($remote_host, $remote_port);    
} catch (Exception $e) {
    die("could not connect to ".$remote_host);
}

try {
    ssh2_auth_password($remote_conn, $remote_user, $remote_pass);
} catch (Exception $e) {
    die("Password failed on ".$remote_host);
}

$sftp = ssh2_sftp($remote_conn);

$fetch_string = "ssh2.sftp://$sftp" . $remote_dir . $remote_file;

//If I add this it says it doesn't exists for some reason even though I can see the file if I log in remotely.
$fileExists = file_exists($fetch_string);
if (!$fileExists) { 
    die('File does not exist');
}

$stream = fopen($fetch_string, 'r');

if (!$stream) {
    die("Could not open remote file: " . $fetch_string . "\n");
}

同样,同样的代码在旧服务器上有效,但在新服务器上无效。我错过了什么?我可以使用ssh2_scp_recv() 轻松地将文件复制到我的服务器上,它工作正常,所以我不确定fopen() 函数发生了什么。

【问题讨论】:

  • 您可以在新服务器上使用相同的用户/密码手动进行身份验证吗? /my/directory/file.txt 是否存在于新服务器上?它有适当的权限吗?
  • 我可以手动进行身份验证,并且连接确实有效。当我说新服务器时,我的意思是脚本所在的位置用于调用另一台服务器。文件所在的另一台服务器根本没有改变。只是托管上述脚本的服务器。
  • 禁用防火墙看看效果(SSH脚本所在的服务器)。哦,您已经可以成功发出 SSH 请求了吧?如果是这样,那可能是@ivanivan 提到的文件权限问题。
  • 托管上述脚本的是哪台服务器?不幸的是,我无法禁用包含该文件的服务器上的防火墙。
  • 你的 SSH 脚本(上面的脚本)所在的服务器 ...

标签: php libssh2 ssh2-sftp


【解决方案1】:

经过数小时尝试不同的选择后,我终于遇到了这个问题。 http://php.net/manual/en/wrappers.ssh2.php

我改变了这个: $fetch_string = "ssh2.sftp://$sftp" . $remote_dir . $remote_file;

对此: $fetch_string = "ssh2.sftp://user:password@mywebsiteorip.com" . $remote_dir . $remote_file;

然后fopen() 函数开始工作了。

我不确定是否在较新的版本中他们只是弃用了 ssh2_sftp() 函数,或者它只是像 @billynoah 提到的一个错误,因为上面的链接甚至没有使用它。

我知道这是多么痛苦,所以希望它能帮助别人!

【讨论】:

  • 这很酷 - 我将不得不在 php7 / wordpress 安装中试一试,看看它是否有帮助。
  • 这应该被标记为正确答案,就像解决方案看起来一样奇怪 - 从 5.4 切换到 5.6.30 后遇到同样的问题 - 没有一个 ssh2_* 方法有效,唯一的解决方案是在 fopen 中使用完整的 url。
【解决方案2】:

这里 $fetch_string = "ssh2.sftp://$sftp" 。 $remote_dir 。 $remote_file;

使用 intval($sftp) 代替 $sftp

【讨论】:

【解决方案3】:

很可能与我最近遇到的 ssh2 扩展中的这个错误有关:

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

这会破坏 Wordpress 中的 ssh2 更新功能:

https://core.trac.wordpress.org/ticket/35517

【讨论】:

  • 非常有趣。谢谢!直到现在我还没有看到。让我进一步挖掘。
  • 我刚刚验证了我可以使用ssh2_scp_recv() 将文件复制到我的服务器,尽管我更喜欢使用fopen()。有什么想法吗?
【解决方案4】:

我也遇到了同样的问题,我很挣扎。对我来说,它现在已修复。
这就是我所做的:
我的 PHP 版本:5.6.25
从以下位置下载了最新的 php_ssh2.dll:https://phpfashion.com/php-ssh2-dll-for-php-5-6-and-7-0(php_ssh2.dll for PHP 5.6 x64)
替换了以下位置的 php_ssh2.dll"C:\wamp64\bin\php\php5.6.25\ext" 并重新启动了我的所有服务。

【讨论】:

    【解决方案5】:

    确保在(Powershell)服务器上启用了 sftp 服务

    【讨论】:

      猜你喜欢
      • 2013-01-29
      • 2021-09-21
      • 2013-01-30
      • 1970-01-01
      • 2012-09-03
      • 2016-06-22
      • 2016-04-16
      • 2017-09-28
      • 2015-11-24
      相关资源
      最近更新 更多