2013-07-26 183 views
0

我尝试编写一个PHP脚本,该脚本可以通过ssh连接到远程服务器并执行命令。 这是我到目前为止的代码:身份验证失败ssh2_auth_pubkey_file()

$ssh = ssh2_connect($ip,22); 
$boolean = ssh2_auth_pubkey_file($ssh, $user, './pubkey.pub', './privatekey.ppk'); 
$stream = ssh2_exec($ssh, $command); 
stream_set_blocking($stream, true); 

以下异常我的脚本返回:

Warning: ssh2_auth_pubkey_file() [function.ssh2-auth-pubkey-file]: Authentication failed for $user using public key in file.php 

当我用油灰和我的私钥,我可以连接没有任何问题连接。

我用PuttyGen生成了我的.ppk文件中的公钥。

任何人都可以帮忙吗?

回答

0

就我个人而言,我认为你最好使用phpseclib, a pure PHP SSH implementation。例如。

<?php 
include('Net/SSH2.php'); 
include('Crypt/RSA.php'); 

$ssh = new Net_SSH2('www.domain.tld'); 
$key = new Crypt_RSA(); 
$key->loadKey(file_get_contents('privatekey')); 
if (!$ssh->login('username', $key)) { 
    exit('Login Failed'); 
} 

echo $ssh->exec('pwd'); 
echo $ssh->exec('ls -la'); 
?> 

其中,它不需要单独的公钥和私钥文件。所以这对你来说是一个潜在的问题。公钥/私钥文件不匹配。

此外,使用phpseclib,您可以启用日志记录功能,并确切地查看它发生故障的位置,而libssh2不提供此类功能。