【发布时间】:2012-06-13 01:29:02
【问题描述】:
在 PHP5.3.3(在 CentOS 和 apache2 上)我试图通过 php 脚本连接到 SFTP。该代码从构造函数中获取密钥和服务器详细信息
function __construct(){
$this->host = 'servername.loc';
$this->port = SFTP_PORT;
$this->auth_user = 'username';
$this->auth_pub = '/data/home/username/.ssh/id_rsa.pub';
$this->auth_priv = '/data/home/username/.ssh/id_rsa';
$this->auth_pass = null;
$this->connection = null;
}
并使用这些详细信息来创建连接。
private function connect(){
if (!($this->connection = ssh2_connect($this->host, $this->port))) {
$this->response = array('code' => "20",
"message" => "Error connecting to SFTP server.");
return false;
}
if (!ssh2_auth_pubkey_file($this->connection, $this->auth_user, $this->auth_pub,
$this->auth_priv, $this->auth_pass)) {
$this->response = array('code' => "40",
"message" => "Error authenticating to SFTP server with key.");
$this->disconnect();
return false;
}
}
我得到的结果是调用ssh2_auth_pubkey_file()时出错。
错误是:
“ssh2_auth_pubkey_file():使用公钥对 USERNAME 进行身份验证失败:密钥数据无效,未经过 base64 编码”
密钥上没有密码,我可以通过 CLI ssh 使用这些密钥手动连接到服务器。
我被难住了。我需要以某种方式对密钥进行编码吗?有什么建议吗?
【问题讨论】:
-
嗯,它数字化了,我在公开提问后找到了答案。在另一个带有 dev cmets 的站点上找到了这个。 d23d23 at gmail dot com 说: “公钥必须在一行上,以密钥类型开头,1 个空格,后跟 keydata(没有换行符),后面不能跟 cmets。这个是 libssh2 的限制,因此请在使用您的密钥生成工具创建文件后从文件中删除任何多余的数据。” 因此,即使我使用 openssl 创建私钥和公钥,我也必须对其进行编辑以放置如上所述,所有这些都与密钥类型放在一行上。谢谢。 Unkul Munki