ไลบรารี PHP ที่ให้ wrapper เชิงวัตถุเพื่อเชื่อมต่อกับ SSH และรันคำสั่งเชลล์ด้วยส่วนขยาย php ssh2
วิธีที่ดีที่สุดในการเพิ่มไลบรารีให้กับโปรเจ็กต์ของคุณคือการใช้ผู้แต่ง
composer require faraweilyas/ sshbunny
หรือ
git clone https://github.com/faraweilyas/sshbunny.git
ตัวสร้าง sshbunny
ใช้เวลาสี่พารามิเตอร์และพวกเขาทั้งหมดมีค่าเริ่มต้น $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
has $ 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 ();