cURL sftp公钥认证失败”;回调错误”;


cURL sftp public key authentication fails "Callback Error"

我有一些php代码,它可以很好地使用cURL将文件上传到只使用user&密码ftp,现在我必须上传到一个只允许公钥身份验证的服务器,并收到错误:"*SSH公钥身份验证失败:回调返回错误"

我对密钥有问题,因为它们的格式不正确,但后来把它们放在了正确的单行格式中,这就阻止了"非base64编码"的错误。我在网上找不到关于这个回调错误的太多帮助。

我的代码如下。

$ch = curl_init();
curl_setopt($ch, CURLOPT_VERBOSE, TRUE); 
curl_setopt($ch, CURLOPT_URL, 'sftp://user:@12.12.12.12:22/testfile.gz');
curl_setopt($ch, CURLOPT_PROTOCOLS, CURLPROTO_SFTP);
curl_setopt($ch, CURLOPT_SSH_PUBLIC_KEYFILE,'C:'keys'public.pub');
curl_setopt($ch, CURLOPT_SSH_PRIVATE_KEYFILE,'C:'keys'private.ppk');
curl_setopt($ch, CURLOPT_SSH_HOST_PUBLIC_KEY_MD5,'2acfe24108c37a276a93ac3398a5oe8f');
curl_setopt($ch, CURLOPT_SSH_AUTH_TYPES,CURLSSH_AUTH_PUBLICKEY);
curl_setopt($ch, CURLOPT_UPLOAD, 1);
$fp = fopen($localfile, 'r');
curl_setopt($ch, CURLOPT_INFILE, $fp);
curl_setopt($ch, CURLOPT_INFILESIZE, filesize($localfile));
$sR = curl_exec ($ch);

这是运行测试的输出

* About to connect() to 12.12.12.12 port 22 (#0)
*   Trying 12.12.12.12...
* connected
* Connected to 12.12.12.12 (12.12.12.12) port 22 (#0)
* SSH MD5 fingerprint: ebbc61b886c798b25073c912833ffers
* SSH authentication methods available: publickey
* Using ssh public key file C:'keys'public.pub
* Using ssh private key file C:'keys'private.ppk
* SSH public key authentication failed: Callback returned error
* Authentication failure
* Closing connection #0

感谢的任何帮助

在某些情况下(基于debian的发行版),使用libgcrypt构建libssh2。在这些情况下,使用PEM编码的私钥文件:

$ openssl rsa -in ~/.ssh/id_rsa -outform pem > id_rsa.pem

ppk是一个putty putty私钥,您需要将其导出为开放(使用puttygen-go-Conversations->export OpenSSH)

您可能会更好地使用phpseclib,一个纯PHP SFTP实现。例如

<?php
include('Net/SFTP.php');
$sftp = new Net_SFTP('www.domain.tld');
if (!$sftp->login('username', 'password')) {
    exit('Login Failed');
}
// puts a three-byte file named filename.remote on the SFTP server
$sftp->put('filename.remote', 'xxx');
?>
Debian发行版中的

libssh2(例如Ubuntu LTS 14.04)使用不支持密码短语的libgcrypt。按照Alexander的回答,使用不带密码的密钥或生成PEM密钥。

有关更多信息,请访问以下链接:尝试使用ssh2_auth_pubkey_file()进行连接