sshbunny
1.0.0
PHP 函式庫提供了一個物件導向的包裝器來連接到 SSH 並使用 php ssh2 擴充功能來執行 shell 命令。
將庫新增至專案的最佳方法是使用 Composer。
composer require faraweilyas/ sshbunny或者
git clone https://github.com/faraweilyas/sshbunny.gitsshbunny建構子有四個參數,它們都有預設值$method='local' 、 $authType=NULL 、 $host=NULL 、 $port=22 、 $username=NULL
$method可以設定為local或remote ,而local將在沒有網路連線的情況下在您自己的 shell 上執行命令,而remote根據您的設定在您連接的遠端伺服器上執行命令。$authType可以設定為KEY 、 PASSWORD或KEY_PASSWORD ,可以設定為KEY和KEY_PASSWORD使用 ssh2_auth_pubkey_file ,差異在於當您設定$authType='KEY_PASSWORD' ssh2_auth_pubkey_file 時, 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 ();