PHP Classes

Phell Friendly PHP Shell Commands: Execute shell commands and return the results

Recommend this page to a friend!
  Info   Documentation   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
Not enough user ratingsTotal: 121 All time: 9,492 This week: 36Up
Version License PHP version Categories
phell 1.1.2BSD License5PHP 5, System information, Unix, Console
Description 

Author

This package can execute shell commands and return the results.

It provides a base abstract class that can execute shell commands by name.

The package also provides several sub-classes that can run common commands available on the Linux distributions and process the output of those commands to returned them in arrays.

Currently it provides command sub-classes to execute and process the output of the Linux commands df, lscpu, lsmem, vmstat, man and free.

A wrapper for obtaining information and deal with common tasks on Linux servers in data formats.

Innovation Award
PHP Programming Innovation award nominee
April 2021
Number 2


Prize: One official elePHPant Plush Mascott
Sometimes applications need to use separate programs to implement a functionality that is necessary to execute an important task for the application users.

PHP can easily execute separate programs as if they were run from the command line shell. However, processing the results of certain types of command can be a complicated task.

This package provides a solution to simplify the processing of the results returned by certain commands by parsing those results and return an array that is easier for the applications to handle.

Manuel Lemos
Picture of Carlos Artur Curvelo da Matos
  Performance   Level  
Name: Carlos Artur Curvelo da ... is available for providing paid consulting. Contact Carlos Artur Curvelo da ... .
Classes: 19 packages by
Country: Portugal Portugal
Age: 46
All time rank: 287339 in Portugal Portugal
Week rank: 215 Up5 in Portugal Portugal Up
Innovation award
Innovation award
Nominee: 13x

Winner: 2x

Documentation

Phell

Linux system monitor utilities offer a comprehensive way of checking and monitoring any server. However, not everyone wants or is able to be checking the command line all times. Also, as those commands use to return unformatted strings, it becomes challenging to process or store such data in a more friendly way.

This wrapper offers classes to execute and retrieve common shell system commands and return data in a PHP-friendly way. Available classes/commands:

  • df
  • man
  • free
  • lscpu
  • lsmem
  • vmstat (no arguments)

Installing

This package can be installed using Composer composer require carloswph/phell

Usage

Using Composer, you need to use the classes correspondent to the shell commands you want data to be gotten and parsed. Additionally, you may want to use the enum class Params(), which provides constants to modify and apply parameters to the shell commands you are working with.

use Phell\Df;
use Phell\Params;

require __DIR__ . '/vendor/autoload.php';

$df = new Df([
	Params::HUMAN_READABLE
]);
var_dump($df->get()); // Getting a parsed array of results for the shell command

/*
RESULTS:

array(10) {
  [0]=>
  array(6) {
    ["filesystem"]=>
    string(4) "udev"
    ["blocks/size"]=>
    string(4) "1.9G"
    ["used"]=>
    string(1) "0"
    ["available"]=>
    string(4) "1.9G"
    ["use_percentage"]=>
    string(2) "0%"
    ["mounted_on"]=>
    string(4) "/dev"
  }
  [1]=>
  array(6) {
    ["filesystem"]=>
    string(5) "tmpfs"
    ["blocks/size"]=>
    string(4) "382M"
    ["used"]=>
    string(4) "1.8M"
    ["available"]=>
    string(4) "380M"
    ["use_percentage"]=>
    string(2) "1%"
    ["mounted_on"]=>
    string(4) "/run"
  }
  [2]=>
  array(6) {
    ["filesystem"]=>
    string(9) "/dev/sda3"
    ["blocks/size"]=>
    string(4) "449G"
    ["used"]=>
    string(3) "31G"
    ["available"]=>
    string(4) "395G"
    ["use_percentage"]=>
    string(2) "8%"
    ["mounted_on"]=>
    string(1) "/"
  }
  [3]=>
  array(6) {
    ["filesystem"]=>
    string(5) "tmpfs"
    ["blocks/size"]=>
    string(4) "1.9G"
    ["used"]=>
    string(4) "156M"
    ["available"]=>
    string(4) "1.8G"
    ["use_percentage"]=>
    string(2) "9%"
    ["mounted_on"]=>
    string(8) "/dev/shm"
  }
  [4]=>
  array(6) {
    ["filesystem"]=>
    string(5) "tmpfs"
    ["blocks/size"]=>
    string(4) "5.0M"
    ["used"]=>
    string(4) "4.0K"
    ["available"]=>
    string(4) "5.0M"
    ["use_percentage"]=>
    string(2) "1%"
    ["mounted_on"]=>
    string(9) "/run/lock"
  }
  [5]=> ... and so on...
}

*/

Using 'man' command

Using the command 'man' is somewhat different than using all the others. As per getting a response, one must provide the bash command for which the manual is required. So, while instantiating the class, you must pass the bash command as parameter.

use Phell\Man;
use Phell\Params;

require __DIR__ . '/vendor/autoload.php';


$cpu = new Man('dir');
echo $cpu->toHtml(); // Generate the response in readable HTML
var_dump($cpu->get()); // Regular response in JSON
var_dump($cpu->toArray()); // Parsed response in PHP array, in which subtitles of the manual are keys

Adding a new command

Of course you can use the main abstract class to manage any other shell command and add new classes. For such, the only mandatory method will be process(), using the protected visibility, and the sole necessary property being $command, in which you will declare your new shell command, like this:

namespace Phell;

/
 * 
 */
class NewCommand extends AbstractCommand
{
	protected $command = 'newcommand';

	protected function process($shell)
	{
		// Process and parsing
	}
}

Additionally, even though you can always find another way of processing additional parameters in your new command, we encourage you to use the enum class Params to do that - as some of the necessary parameters could be possibly around there yet.

Commands available

This package in a work in progress, so new commands will be added eventually. Keep checking the README file and updating the package to get access to all of them.

Available commands: df, free, lscpu, lsmem, vmstat

Underway: iostat, uptime, groups


  Files folder image Files (13)  
File Role Description
Files folder image.github (1 directory)
Files folder imagesrc (9 files)
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file composer.lock Data Auxiliary data
Accessible without login Plain text file README.md Doc. Documentation

  Files folder image Files (13)  /  .github  
File Role Description
Files folder imageworkflows (1 file)

  Files folder image Files (13)  /  .github  /  workflows  
File Role Description
  Accessible without login Plain text file codacy-analysis.yml Data Auxiliary data

  Files folder image Files (13)  /  src  
File Role Description
  Plain text file AbstractCommand.php Class Class source
  Plain text file Df.php Class Class source
  Plain text file Free.php Class Class source
  Plain text file Helpers.php Class Class source
  Plain text file Lscpu.php Class Class source
  Plain text file Lsmem.php Class Class source
  Plain text file Man.php Class Class source
  Plain text file Params.php Class Class source
  Plain text file Vmstat.php Class Class source

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
 Version Control Unique User Downloads Download Rankings  
 100%
Total:121
This week:0
All time:9,492
This week:36Up
User Comments (1)
Gongratulations, thats a great job for linux lovers ;-)
3 years ago (José Filipe Lopes Santos)
70%StarStarStarStar