简体   繁体   English

如何从PHP脚本执行命令行

[英]How to do command line from PHP script

I need to write a script that will give users info on a given Unix account (the same Unix server that the script lives on). 我需要编写一个脚本,该脚本将为用户提供给定Unix帐户(脚本所在的Unix服务器)上的信息。 Mostly thing kinds of things that are in the passwd file or available via finger. 多数情况是passwd文件中或可以通过finger获得的东西。

PHP is in safe-mode, so I can't access the passwd file via something built into php like file_get_contents() . PHP处于安全模式,因此我无法通过php中内置的诸如file_get_contents()类的东西来访问passwd文件。 Also, because it's in safe mode, various other command-line functions are disabled. 另外,由于处于安全模式,因此禁用了其他各种命令行功能。

I thought I could get the info via a socket (no clue yet what that means, but I thought I'd try) but I get a fatal error that socket_create() is an unknown function. 我以为可以通过套接字获取信息(尚不知道那是什么意思,但是我想我会尝试的),但是我收到一个致命错误,即socket_create()是一个未知函数。 I pulled up the php-config file (which I can't change, FYI), and sure enough, sockets are not enabled. 我拉起了php-config文件(仅供参考,我无法更改),并且确实可以确定,套接字未启用。

However, while I was in there, I saw the line '--with-exec-dir=' with no actual directory set. 但是,当我在那儿时,我看到没有设置实际目录的行'--with-exec-dir='

So then I remembered that when I was trying EVERY command line function, that some threw "not allowed in safe-mode" type errors, while others did nothing at all. 因此,我想起了当我尝试每个命令行功能时,有些人抛出了“在安全模式下不允许的”类型错误,而另一些则什么也没有做。 If I put something like: 如果我输入类似的内容:

echo "[[";
exec("finger user");
echo "]]";

I'd end up with [[]] . 我最终会得到[[]] So no errors, just no results either. 因此,没有错误,也没有结果。

Bottom line: 底线:

Is there something I haven't tried? 有没有我尝试过的东西? (in general) Is there a runtime config option I can set to make exec() work? (通常)是否可以设置运行时配置选项以使exec()工作?

quick note: I tried passthru() as well, specifically passthru("pwd") with still no output. 快速说明:我也尝试过passthru() ,特别是passthru("pwd")仍然没有输出。

update 更新

based on feedback, I tried both of the following: 根据反馈,我尝试了以下两种方法:

$stuff = exec("pwd", $return);

echo "stuff=".$stuff."\n";
echo "return=";
print_r($return);

which results in: 结果是:

stuff=
return=Array
(
)

and

$stuff = passthru("pwd", $return);

echo "stuff=".$stuff."\n";
echo "return=";
print_r($return);

which results in: 结果是:

stuff=
return=1

The 1 sounds hopeful, but not what I want yet. 1听起来很有希望,但还不是我想要的。

Idea 理念

So this is actually an update of an already existing script that (please don't ask) I don't have access to. 因此,这实际上是对我无权访问的现有脚本的更新(请不要问)。 It's a perl script that's called via cgi. 这是一个通过cgi调用的perl脚本。 Is there a way to do php via cgi (so I don't have to deal with perl or rely on the older code)? 有没有一种方法可以通过cgi做php(因此我不必处理perl或依靠旧代码)?

I'm afraid you can't do that in safe-mode. 恐怕您无法在安全模式下执行此操作。 You have to remove the safe-mode if you have control of the server configuration. 如果您可以控制服务器配置,则必须删除安全模式。

I think you can't rely on sockets to read local files, sockets are used for network related things. 我认为您不能依赖套接字读取本地文件,套接字用于与网络相关的事情。

Exec returns a value, so do: Exec返回一个值,因此:

$var = exec("finger user"); 

and then parse the output to get what you want. 然后解析输出以获得所需的内容。 You can get return status by adding in an optional variable thus: 您可以通过添加可选变量来获得返回状态,从而:

exec("finger user", $var, $return_status);

or just: 要不就:

echo exec("finger user");

if all you want is to see the output. 如果您只想看输出。

exec doesn't inherently return any data. exec本质上不会返回任何数据。

Try something like, 尝试类似的东西,

exec("finger user",$output);

echo "[[";
foreach($output as $key => $value){
echo $value;
}
echo "]]";

Thanks to all that responded, the following is what finally worked: 多亏了所有的回应,以下才是最终成功的方法:

  • Create a cgi-bin folder 创建一个cgi-bin文件夹
  • Add the following to the top of the php script: 将以下内容添加到php脚本的顶部:

      #!/usr/local/bin/php-cgi 

I don't know if this is something special on my server configuration, but I can run exec() and get what I'm after. 我不知道这在我的服务器配置上是否很特别,但是我可以运行exec()并得到我想要的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM