php 使用php执行shell脚本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15060708/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 08:28:35  来源:igfitidea点击:

Executing shell script using php

phpshellshell-exec

提问by Sparky

I have a shell script deploy.shthat has the following content:-

我有一个deploy.sh包含以下内容的 shell 脚本:-

echo "0 Importing the code"
eval "git pull -u origin master"

echo "1 Backing up existing data in database.."
// -- other code follows here

When I execute the script directly using the terminal, I get the following output:-

当我直接使用终端执行脚本时,我得到以下输出:-

0 Importing the code
remote: Counting objects: 5, done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 3 (delta 1), reused 0 (delta 0)
Unpacking objects: 100% (3/3), done.
From bitbucket.org:user/repo
 * branch            master     -> FETCH_HEAD
Updating db13xxx..6705xxx
1 Backing up existing data in database..

This is correct. However, I wrote a PHP script with which I can invole the deploy.sh script over http. Content of this php page is as follows:-

这是对的。但是,我编写了一个 PHP 脚本,我可以使用它通过 http 调用 deploy.sh 脚本。这个php页面的内容如下:-

$output = `./deploy.sh`;
echo '<pre>', $output, '</pre>';

When I invoke this php file through the browser, the shell script is in fact getting invoked and I'm getting the following output:-

当我通过浏览器调用这个 php 文件时,shell 脚本实际上被调用了,我得到了以下输出:-

0 Importing the code
1 Backing up existing data in database..

The problem is that the eval "git pull -u origin master"command didnt get executed and its output is not shown. Any idea what the problem is?

问题是eval "git pull -u origin master"命令没有被执行,它的输出没有显示。知道问题是什么吗?

回答by Swetha

This works

这有效

<?php
$output = shell_exec('sh deploy.sh');
echo "$output";
?>

Before that make sure that file has chmod 777permission.

在此之前,请确保该文件具有chmod 777权限。

回答by ChrisK

You should try to avoid running shell commands in php.

您应该尽量避免在 php 中运行 shell 命令。

Having said that, try this:

话虽如此,试试这个:

$output = shell_exec('./deploy.sh');
echo "<pre>".$output."</pre>";

As per: http://www.php.net/manual/en/function.shell-exec.php

根据:http: //www.php.net/manual/en/function.shell-exec.php

回答by Garet Claborn

One thing you can do with the exec()function is to pass two optional values for more insight.

您可以使用该exec()函数做的一件事是传递两个可选值以获得更多信息。

Here's some code I use to test shell scripts from a web interface.

这是我用来从 Web 界面测试 shell 脚本的一些代码。

<?php
require_once(__DIR__.'/../libs/Render.php');
error_reporting(E_ALL);


//Initialize and Run Command, with a little trick to avoid certain issues
$target='cd ../../your/relative/path && ./CustomScript.sh';
$outbuf=exec($target,$stdoutbuf, $returnbuf);


//Structure
$htm=                           new renderable('html');
$html->children[]=  $head=      new renderable('head');
$html->children[]=  $body=      new renderable('body');
$body->children[]=  $out=       new renderable('div');
$body->children[]=  $stdout=    new renderable('div');
$body->children[]=  $returnout= new renderable('div');


//Value
$out->content=         'OUTPUT: '.$outbuf;
$stdout->content=      'STDOUT: '.var_export($stdoutbuf,true);
$returnout->content=   'RETURN: '.$returnbuf; //127 == Pathing problem


//Output
print_r($html->render());
?>

File is using the renderable classfrom the project I use this in, but you can put the string output wherever you are using it or echo/print_r()just as well. Also make sure you're not in safe mode by running phpinfo(); lots of folks having that issue.

File 正在使用我在其中使用它的项目中的可渲染类,但是您可以将字符串输出放在任何使用它的地方,或者也可以放在任何地方echo/print_r()。还要确保您没有通过运行 phpinfo(); 进入安全模式;很多人有这个问题。

Additionally, there's no reason you should avoid using shell scripts in PHP. PHP being a scripting language, it is quite thrifty at aggregating many shell scripts to allow higher-level administration.

此外,您没有理由避免在 PHP 中使用 shell 脚本。PHP 作为一种脚本语言,它在聚合许多 shell 脚本以允许更高级别的管理方面非常节俭。

PHP isn't only for 'web sites'. Even then, exposing administrative scripts to web interfaces is quite useful in and of itself; occasionally this is even a project requirement.

PHP 不仅适用于“网站”。即便如此,将管理脚本暴露给 Web 界面本身就非常有用。有时,这甚至是项目要求。

回答by Vikash Jha

This is the correct code

这是正确的代码

<?php 
  $cmd = 'ifconfig'; // pass command here
  echo "<pre>".shell_exec($cmd)."</pre>";
?>