使用 PHP 执行 unix shell 命令

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5631600/
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 22:02:20  来源:igfitidea点击:

Executing unix shell commands using PHP

phpshellunix

提问by user478636

A text box will be used to capture the command. I've been told that I have to use the exec()function to execute UNIX shell commands.

文本框将用于捕获命令。有人告诉我,我必须使用该exec()函数来执行 UNIX shell 命令。

Something like this, user types ls in the text box. The exec()function will execute the UNIX command and the command will be displayed on the web page.

像这样,用户在文本框中键入 ls。该exec()函数将执行 UNIX 命令,该命令将显示在网页上。

What I want to know how will I get the output of the shell command and display in the web browser using PHP.

我想知道如何使用 PHP 获取 shell 命令的输出并在 Web 浏览器中显示。

I don't know where to start since I'm very new to PHP.

我不知道从哪里开始,因为我对 PHP 很陌生。

I'm using Ubuntu.

我正在使用 Ubuntu。

采纳答案by enricog

You could start looking at the php manual:

你可以开始看php手册:

System program execution

系统程序执行

But like sdleihssirhc mentioned, watchout this IS very dangerous and you should NOTallow everything to be executed!
If you still want to do it, to get the output of the shell, just use

但是,像sdleihssirhc提到,WATCHOUT,这是非常危险的,你应该允许执行的一切!
如果您还想这样做,要获取shell的输出,只需使用

exec
The output of the shell will be passed in the second parameter.

exec
shell 的输出将传入第二个参数。

E.g.:

例如:

exec('ls -la', $outputArray);
print_r($outputArray);

回答by Charles

exec?

exec?

system?

system?

shell_exec?

shell_exec?

passthru?

passthru?

Backticks?

反引号?

Pfah!

噗!

Realdevelopers use proc_open! It has the major and distinct advantage of giving you three PHP streams to feed data into the process, and read bothstdoutand stderr. This is something that the other process execution functions simply don't do well.

真正的开发者使用proc_open!它有让你们三个PHP流将数据提供给该过程,并宣读了重大和明显的优势stdoutstderr。这是其他流程执行功能根本没有做好的事情。

It comes at the small cost of some boilerplate code, so it's a bit more verbose. I consider the trade-off to be excellent.

它以一些样板代码的小成本为代价,所以它有点冗长。我认为这种权衡非常好。

Oh, and running arbitrary commands from your users is perhaps one of the greatest security risks that you could ever conceive of,but I kind of assume you know this by now.

哦,从你的用户那里运行任意命令可能是你能想到的最大的安全风险之一,但我假设你现在知道这一点。

回答by ThiefMaster

Use $output = system($command);

$output = system($command);

See http://php.net/systemand don't forget to read the warnings about security. If you let a user pass any data to system()(or exec()etc.) it's almost as if they had a shell on your server. The same applies if you don't sanitize arguments passed to programs executed through these functions properly.

请参阅http://php.net/system并且不要忘记阅读有关安全性的警告。如果您让用户将任何数据传递给system()(或exec()等),就好像他们在您的服务器上有一个 shell。如果您没有正确清理传递给通过这些函数执行的程序的参数,则同样适用。

回答by Teneff

Try $output = shell_exec('ls -lart');

尝试 $output = shell_exec('ls -lart');

doc shell_exec

doc shell_exec

回答by gnur

As long as it is one line you can just echothe return value of exec.

只要是一行就可以只echo返回exec.

Like so:

像这样:

echo exec('ls');

echo exec('ls');

But it only displays the first line.

但它只显示第一行。

回答by Peeter

exec(escapeshellarg($userSuppliedInput), $output);

echo $output;

回答by Richard Tuin

You can use the backticks for this purpose. Like:

为此,您可以使用反引号。喜欢:

$output = `command-executable -switches`

In addition, some applications echo their output to the STD_ERR stream so you might not see output. On linux, you can redirect the error input to the 'normal' input by appending 2>&1to the command string.

此外,某些应用程序将其输出回显到 STD_ERR 流,因此您可能看不到输出。在 linux 上,您可以通过附加2>&1到命令字符串将错误输入重定向到“正常”输入。