faraweilyas/sshbunny 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

faraweilyas/sshbunny

Composer 安装命令:

composer require faraweilyas/sshbunny

包简介

PHP library that provides an object-oriented wrapper to connect to SSH and run shell commands with the php ssh2 extension.

README 文档

README

PHP library that provides an object-oriented wrapper to connect to SSH and run shell commands with the php ssh2 extension.

Requirements

Install with composer

The best way to add the library to your project is using composer.

composer require faraweilyas/sshbunny

or

Clone this repo

git clone https://github.com/faraweilyas/sshbunny.git

Configuration

SSHBunny constructor takes four parameters and they all have default values $method='local', $authType=NULL, $host=NULL, $port=22, $username=NULL

  • $method can be set to local or remote, local will execute commands on your own shell without internet connection while remote executes commands on the remote server that you connect to based on your configuration.
  • $authType can be set to KEY, PASSWORD or KEY_PASSWORD, KEY and KEY_PASSWORD uses ssh2_auth_pubkey_file the difference is when you set $authType='KEY_PASSWORD' ssh2_auth_pubkey_file takes the last parameter of password which will now be required and PASSWORD uses ssh2_auth_password.
  • $port should be set to your server port if your are connecting to a remote server.
  • $username should be set to your server username.

if your are setting connection method to $method='remote' and $authType = KEY || KEY_PASSWORD that means you will need to set your public & private key file which you can do with the setters SSHBunny has $sshBunny->setKeys('public_key.pub', 'private_key') before initialization.

Basic usage

This is just going to run locally since connection method is set to 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);

This is going connect to a remote server since connection method is set to remote and authentication type is set to 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);

Command execution can take multiple commands or you can chain on the exec method with another exec method

$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);

Available methods

  • Executed command output
// 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);
  • Clear stored executed command output
// Will clear the first executed command output and return the next executed command output
$sshBunny
    ->exec("ls -la")
    ->clearData()
    ->exec("whoami")
    ->getData(TRUE);
  • Disconnect server connection
// Will run the commands provided and display the result then disconnect from the server
$sshBunny
    ->exec("ls -la", "whoami")
    ->getData(TRUE)
    ->disconnect();

faraweilyas/sshbunny 适用场景与选型建议

faraweilyas/sshbunny 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 83 次下载、GitHub Stars 达 5, 最近一次更新时间为 2018 年 11 月 24 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「shell」 「ssh」 「ssh connector」 「shell commands」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 faraweilyas/sshbunny 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 faraweilyas/sshbunny 我们能提供哪些服务?
定制开发 / 二次开发

基于 faraweilyas/sshbunny 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 83
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 5
  • 点击次数: 12
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 5
  • Watchers: 2
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2018-11-24