[PHP] PHPで秘密鍵を使ってSSH認証する

PHPで秘密鍵文字列からSSH認証する方法。

PECLのssh2をインストールしている前提とする。

ssh2をインストールする方法は、他に参考サイトがあるので割愛するが、

  • php-fedora-atuloader
  • php-pear
  • php-devel
  • libssh2
  • libssh2-devel
  • gcc

この辺りをインストールしとけば、pecl install ssh2 で入ったと思う。

あとは、extensionに設定して、apache再起動。

後は以下の通り

ssh2_auth_pubkey_fileには、実体の鍵ファイルが必要なので、file_put_contentsで実体化して、execでLinuxコマンドのssh-keygenで公開鍵を秘密鍵から作成している。

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">