dongxikuo5171 2014-02-09 14:02
浏览 145
已采纳

无法使用ssh2_auth_pubkey_file连接到php中的EC2

I'm trying to connect my EC2 instance with ssh2 php module.

I've obtained the public key from known_hosts file (format: "ssh-rsa XXXX..." putted into "public.txt" file) and i'm using the amazon keypair.pem as private key ("-----BEGIN RSA PRIVATE KEY----- XXXX..." in private.pem).

I'm using this php code:

$connection = ssh2_connect('XXXX.eu-west-1.compute.amazonaws.com', 22, array('hostkey'=>'ssh-rsa'));    
if(!ssh2_auth_pubkey_file(
    $connection,
    'ubuntu',
    'public.txt',
    'private.pem'
))
    die("Auth error");  
$sftp = ssh2_sftp($connection);

The error i get is: ssh2_auth_pubkey_file(): Authentication failed for ubuntu using public key: Username/PublicKey combination invalid

What am i doing wrong?

  • 写回答

1条回答 默认 最新

  • dougang6178 2014-02-10 19:48
    关注

    The public key in the known_hosts file isn't the public key you need to be using. Rather, you need to be using the public key from authorized_keys.

    That said, the fact that libssh2 even requires the public key is stupid. Honestly, I'd recommend you use phpseclib, a pure PHP SSH2 implementation, over libssh2. eg.

    <?php
    include('Net/SFTP.php');
    include('Crypt/RSA.php');
    
    $ssh = new Net_SFTP('www.domain.tld');
    $key = new Crypt_RSA();
    $key->loadKey(file_get_contents('private.pem'));
    if (!$ssh->login('username', $key)) {
        exit('Login Failed');
    }
    
    print_r($ssh->nlist());
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥17 pro*C预编译“闪回查询”报错SCN不能识别
  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?