Biblioteca PHP que proporciona un contenedor orientado a objetos para conectarse a SSH y ejecutar comandos de shell con la extensión php ssh2.
La mejor manera de agregar la biblioteca a su proyecto es usando Composer.
composer require faraweilyas/ sshbunny
o
git clone https://github.com/faraweilyas/sshbunny.git
El constructor sshbunny
toma cuatro parámetros y todos tienen valores predeterminados $method='local'
, $authType=NULL
, $host=NULL
, $port=22
, $username=NULL
$method
se puede configurar en local
o remote
, local
ejecutará comandos en su propio shell sin conexión a Internet, mientras que remote
ejecuta comandos en el servidor remoto al que se conecta según su configuración.$authType
se puede configurar en KEY
, PASSWORD
o KEY_PASSWORD
, KEY
y KEY_PASSWORD
usan ssh2_auth_pubkey_file la diferencia es cuando configura $authType='KEY_PASSWORD'
ssh2_auth_pubkey_file toma el último parámetro de contraseña que ahora será requerido y PASSWORD
usa ssh2_auth_password.$port
debe configurarse en el puerto de su servidor si se está conectando a un servidor remoto.$username
debe configurarse con el nombre de usuario de su servidor. si está configurando el método de conexión en $method='remote'
y $authType = KEY || KEY_PASSWORD
eso significa que necesitará configurar su archivo de clave pública y privada, lo cual puede hacer con los configuradores sshbunny
tiene $ sshbunny ->setKeys('public_key.pub', 'private_key')
antes de la inicialización.
Esto solo se ejecutará localmente ya que el método de conexión está configurado en 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 );
Esto se conectará a un servidor remoto ya que el método de conexión está configurado en remote
y el tipo de autenticación está configurado en 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 );
La ejecución de comandos puede tomar varios comandos o puede encadenar el método exec
con otro método 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 ();