sshbunny
1.0.0
SSH에 연결하고 php ssh2 확장을 사용하여 셸 명령을 실행하기 위한 객체 지향 래퍼를 제공하는 PHP 라이브러리입니다.
프로젝트에 라이브러리를 추가하는 가장 좋은 방법은 작곡가를 사용하는 것입니다.
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')
가 있는 setter를 사용하여 공개 및 개인 키 파일을 설정해야 함을 의미합니다.
연결 방법이 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 ();