sshbunny
1.0.0
SSH に接続し、php ssh2 拡張子を使用してシェル コマンドを実行するためのオブジェクト指向ラッパーを提供する PHP ライブラリ。
プロジェクトにライブラリを追加する最良の方法は、composer を使用することです。
composer require faraweilyas/ sshbunny
または
git clone https://github.com/faraweilyas/sshbunny.git
sshbunny
コンストラクターは 4 つのパラメーターを受け取り、それらはすべてデフォルト値$method='local'
、 $authType=NULL
、 $host=NULL
、 $port=22
、 $username=NULL
を持ちます。
$method
local
またはremote
に設定できます。 local
インターネットに接続せずに独自のシェルでコマンドを実行し、 remote
設定に基づいて接続するリモートサーバーでコマンドを実行します。$authType
KEY
、 PASSWORD
、またはKEY_PASSWORD
に設定できます。 KEY
とKEY_PASSWORD
ssh2_auth_pubkey_file を使用します。違いは、 $authType='KEY_PASSWORD'
に設定した場合、 ssh2_auth_pubkey_file は必須となるパスワードの最後のパラメータを取得し、 PASSWORD
ssh2_auth_password を使用することです。$port
サーバー ポートに設定する必要があります。$username
サーバーのユーザー名に設定する必要があります。 || 接続方法を$method='remote'
および$authType = KEY || KEY_PASSWORD
は、初期化の前に公開キーと秘密キーのファイルを設定する必要があることを意味します。これは、 sshbunny
の$ sshbunny ->setKeys('public_key.pub', 'private_key')
セッターを使用して行うことができます。
接続方法がlocal
に設定されているため、これはローカルで実行されるだけです
<?php
use sshbunny sshbunny ;
require_once ' vendor/autoload.php ' ;
// ->getData() will return output of command executed while ->getData(TRUE) will dispay the output
$ sshbunny = ( new sshbunny ( ' local ' ))
-> initialize ()
-> exec ( " echo 'Hello World' " )
-> getData ( TRUE );
接続方法がremote
、認証タイプがKEY
に設定されているため、これはリモート サーバーに接続します。
<?php
use sshbunny sshbunny ;
require_once ' vendor/autoload.php ' ;
defined ( ' TEST_HOST ' ) ? NULL : define ( ' TEST_HOST ' , " 138.222.15.1 " );
defined ( ' PORT ' ) ? NULL : define ( ' PORT ' , " 22 " );
defined ( ' USERNAME ' ) ? NULL : define ( ' USERNAME ' , " ubuntu " );
defined ( ' PUBLIC_KEY ' ) ? NULL : define ( ' PUBLIC_KEY ' , ' id_ssl.pub ' );
defined ( ' PRIVATE_KEY ' ) ? NULL : define ( ' PRIVATE_KEY ' , ' id_ssl ' );
$ sshbunny = ( new sshbunny ( ' remote ' , ' KEY ' , HOST , PORT , USERNAME ))
-> setKeys ( PUBLIC_KEY , PRIVATE_KEY )
-> initialize ()
-> exec ( " echo 'Hello World' " )
-> getData ( TRUE );
コマンドの実行には複数のコマンドを使用することも、 exec
メソッドを別のexec
メソッドと連鎖させることもできます
$ sshbunny = ( new sshbunny ( ' remote ' , ' KEY ' , HOST , PORT , USERNAME ))
-> setKeys ( PUBLIC_KEY , PRIVATE_KEY )
-> initialize ()
// Multiple commands
-> exec ( " echo 'Hello World' " , " cd /var/www/html " )
// Method chaining
-> exec ( " ls -la " )
-> getData ( TRUE );
// Will return the result of executed command output
$ sshbunny
-> exec ( " ls -la " )
-> getData ();
// Will display the result of executed command output
$ sshbunny
-> exec ( " ls -la " )
-> getData ( TRUE );
// Will clear the first executed command output and return the next executed command output
$ sshbunny
-> exec ( " ls -la " )
-> clearData ()
-> exec ( " whoami " )
-> getData ( TRUE );
// Will run the commands provided and display the result then disconnect from the server
$ sshbunny
-> exec ( " ls -la " , " whoami " )
-> getData ( TRUE )
-> disconnect ();