在 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 使用这些密钥手动连接到服务器。
我难住了。我需要以某种方式对密钥进行编码吗?建议?