English 中文(简体)
• 如何从PHP执行互动指挥?
原标题:How to execute an interactive command from PHP?

我需要执行kdiff3,在我的台式机(当地机)上,从PHP书写(使用浏览器,而不是指挥线)。 我先允许用户www-data,即使用visudo执行 k3。 事实上,如果我登录在www-data。 我可以在没有问题的情况下执行(sudo kdiff3 ......,它完全不要求密码)。

问题在于,我试图用购买力平价文字执行这一指挥。 我试图这样做:

$output = shell_exec("sudo kdiff3 -m $file.def.old $file $file.def -o $file");

并且没有发生(产出是民族解放军)。 如果我尝试一种非互动的指挥,例如ls,它就可运作:

$output = shell_exec("ls");

发生了什么? 为什么不能执行互动指挥?

最佳回答
问题回答

Try passthru($cmd);

它将允许终端屏幕上的用户I/O。

Edit (27 january 2024)

<>条码>是一个更好的选择。

例如:

<?php
$answered = NULL;
$yes = NULL;
while(!$answered) {
    $input = strtoupper(readline( Confirm (Y/N):  ));
    $answered = in_array($input, [ Y ,  N ]);
    $yes = !$answered ?: $input ===  Y ;
    var_dump($answered);
}
var_dump($yes);

问题是,正如你所说的那样,<代码>sudo正在等待用户的投入,是“互动的”,它要求你写一个密码。

可以通过以下方式提供:proc_ open(,执行指挥并分离I/O streams,以便你能够按要求分配产出和提供密码。

I have come across scripts that use this approach of echo "suPassword " | sudo cmd to do this, but I personally have found this to be a bit hit and miss.

引自:

(美元)

每个互动指挥部门都期望有一个工作平台——没有在<代码>shell_exec(>上提供。

使用<代码>popen()和朋友或从档案中转调你的投入。 如果有关文件对





相关问题
Signed executables under Linux

For security reasons, it is desirable to check the integrity of code before execution, avoiding tampered software by an attacker. So, my question is How to sign executable code and run only trusted ...

encoding of file shell script

How can I check the file encoding in a shell script? I need to know if a file is encoded in utf-8 or iso-8859-1. Thanks

How to write a Remote DataModule to run on a linux server?

i would like to know if there are any solution to do this. Does anyone? The big picture: I want to access data over the web, using my delphi thin clients. But i´would like to keep my server/service ...

How can I use exit codes to run shell scripts sequentially?

Since cruise control is full of bugs that have wasted my entire week, I have decided the existing shell scripts I have are simpler and thus better. Here is what I have so far svn update /var/www/...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

热门标签