PHP-Bibliothek, die einen objektorientierten Wrapper zum Herstellen einer Verbindung zu SSH und zum Ausführen von Shell-Befehlen mit der PHP-Erweiterung ssh2 bereitstellt.
Der beste Weg, die Bibliothek zu Ihrem Projekt hinzuzufügen, ist die Verwendung von Composer.
composer require faraweilyas/ sshbunny
oder
git clone https://github.com/faraweilyas/sshbunny.git
sshbunny
-Konstruktor benötigt vier Parameter und alle haben die Standardwerte $method='local'
, $authType=NULL
, $host=NULL
, $port=22
, $username=NULL
$method
kann auf local
oder remote
gesetzt werden. local
führt Befehle auf Ihrer eigenen Shell ohne Internetverbindung aus, während remote
Befehle auf dem Remote-Server ausführt, mit dem Sie sich basierend auf Ihrer Konfiguration verbinden.$authType
kann auf KEY
, PASSWORD
oder KEY_PASSWORD
gesetzt werden. KEY
und KEY_PASSWORD
verwenden ssh2_auth_pubkey_file. Der Unterschied besteht darin, dass Sie $authType='KEY_PASSWORD'
festlegen. ssh2_auth_pubkey_file verwendet den letzten Parameter des Kennworts, der jetzt erforderlich ist, und PASSWORD
verwendet ssh2_auth_password.$port
sollte auf Ihren Server-Port eingestellt sein, wenn Sie eine Verbindung zu einem Remote-Server herstellen.$username
sollte auf Ihren Server-Benutzernamen eingestellt sein. wenn Sie die Verbindungsmethode auf $method='remote'
und $authType = KEY || KEY_PASSWORD
festlegen $authType = KEY || KEY_PASSWORD
, das bedeutet, dass Sie Ihre öffentliche und private Schlüsseldatei festlegen müssen, was Sie mit den Settern sshbunny
has $ sshbunny ->setKeys('public_key.pub', 'private_key')
vor der Initialisierung tun können.
Dies wird nur lokal ausgeführt, da die Verbindungsmethode auf local
eingestellt ist
<?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 );
Dadurch wird eine Verbindung zu einem Remote-Server hergestellt, da die Verbindungsmethode auf remote
und der Authentifizierungstyp auf KEY
eingestellt ist
<?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 );
Die Befehlsausführung kann mehrere Befehle erfordern oder Sie können die exec
-Methode mit einer anderen exec
-Methode verketten
$ 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 ();