如何将PHP前端与makefile / bash脚本连接?

问题描述:

I'm writing a project to manage multiple SSH tunnel via CLI and a web UI.

Bash script

The service is written in Bash and can:

  • start a given tunnel or all tunnels,
  • stop a given tunnel or all tunnels,
  • status of a given tunnel or all tunnels,
  • list all tunnels available.

Makefile script

The makefile can do some "administrative" tasks:

  • add-host add config for a tunnel,
  • remove-host add config for a tunnel,
  • etc.

Web UI

I want to create a web interface –using PHP/CodeIgniter– to control the service.

Question

  • How can I interface CodeIgniter with makefile and bash command ?
  • Specifically how do I use exit status to give information to the PHP script ?

我正在编写一个项目,通过 CLI em>管理多个SSH隧道和一个Web UI 。 p>

Bash脚本 h2>

该服务是用 Bash code>编写的,可以: p>

  • start code>给定隧道或所有隧道, li>
  • 停止 code>给定隧道或所有隧道, li> \ n
  • 状态 code>给定隧道或所有隧道, li>
  • list code>所有隧道可用。 li> ul >

    Makefile脚本 h2>

    makefile code>可以执行一些“管理” em>任务: p> \ n

    • add-host code>为隧道添加配置, li>
    • remove-host code>为a添加配置 隧道, li>
    • 等。 li> ul>

      Web UI h2>

      我想创建一个Web界面 - 使用 PHP code> / CodeIgniter code> - 来控制服务。 p>

      问题 h2>
      • 如何使用makefile和bash命令连接 CodeIgniter em>? li>
      • 具体如何使用退出状态为PHP脚本提供信息? li> \ n ul> div>

PHP's exec (docs here) command will probably do what you need.

You can run your scripts using exec and capture both the output of the script and the exit code. For example:

$output = array();
$exitCode = null;

exec("/path/to/my/script arg1 arg2 2>&1", $output, $exitCode);

$output will be an array of strings - the lines from the script's output. $exitCode will be the exit code returned by the script.

Note, however, that use of exec raises some concerns:

  1. security - you must be careful to ensure that the user of your app cannot execute arbitrary code on your server.

  2. permissions - commands run via exec will be run as the web server user; for Apache on Debian/Ubuntu this will be www-data.