coffeemaru / shellos
一个暴露简单界面的 PHP 系统调用库
1.2.0
2022-09-15 19:55 UTC
Requires
- php: >=7.4
- phpseclib/phpseclib: ~3.0
Requires (Dev)
- phpunit/phpunit: ^9.5
This package is auto-updated.
Last update: 2024-09-16 00:27:00 UTC
README
一个易于使用的 SDK,用于处理系统命令调用。
简单的命令调用
要创建任何命令,我们将使用 shell 函数,这个函数创建一个实例,可以用来执行命令。
$command = shell("ls") if($command->execute()) { echo $command->getOutputString(); }
命令在 shell 调用中不会执行,要执行命令,我们需要使用 execute 方法。如果命令结果是成功代码,该方法返回 true。可以使用 getOutputString 方法获取命令返回的所有输出,如果我们想获取命令的每一行,可以使用返回包含每行输出的数组的 getOutputLines 方法。
$command = shell("ls") if($command->execute()) { for($command->getOutputLines() as $line){ echo $line; } }
SSH 连接。
Shellos 支持 SSH 远程执行,通过 SSHExecutor。如果 SSHExecutor 被配置为默认执行器,函数将通过 SSH 隧道发送命令,而不是在本地执行。
# first we need to inicialice the client with the server credentials. $exec = new SSHExecutor($host, $port, $username, $password) ShellCommand::setDefaultExecutor($exec); # below this code all the `shell` functions call wills raise a ssh command on the remote host. $command = ssh("ls -la") if($command->execute()) { # <-- raise a ssh command }