使用 azure cli 的 php exec 命令始终返回 null

发布于 2025-01-15 03:00:53 字数 304 浏览 2 评论 0原文

在运行 PHP 的 Apache 服务器上,我有一个始终返回 null 的命令。

exec("az 存储容器存在 --account-name $accountName --account-key $key --name $containeurName", $output);

我修改了 sudoers 文件,但它没有改变任何内容:

www-data ALL=(ALL) NOPASSWD: /usr/bin/az

感谢您的帮助。

On a server Apache which runs PHP, I have a command which always return null.

exec("az storage container exists --account-name $accountName
--account-key $key --name $containeurName", $output);

I modified the sudoers file but it didn't change anything:

www-data ALL=(ALL) NOPASSWD: /usr/bin/az

Thanks for your help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

太阳公公是暖光 2025-01-22 03:00:53

使用 exec($your_command, $output, $error_code) 并查看 $error_code 包含什么内容。这可能只是因为 az 不在 PHP 的 PATH 环境变量中。

尝试输入可执行文件的完整路径,通常如下所示:

<?php

// A default path in case "which az" doesn't work.
define('AZ_DEFAULT_PATH', '/usr/bin/az');

// Find the path to az with the "which" command.
$az_path = exec('which az');
if ($az_path === false) {
  $az_path = AZ_DEFAULT_PATH;
}

// Sanitize your variables to avoid shell injection and build the command.
$cmd = $az_path .
       "storage container exists --account-name $accountName " .
       "--account-key $key --name $containeurName";

$last_line = exec($cmd, $full_output, $error_code);

// Then check if $last_line !== false and check $error_code to see
// what happened.
var_export([
  '$cmd' => $cmd,
  '$last_line' => $last_line,
  '$full_output' => $full_output,
  '$error_code' => $error_code,
]);

Use exec($your_command, $output, $error_code) and see what $error_code contains. It may just be because az isn't in the PATH env variable in PHP.

Try to put the full path to your executable, typically something like this:

<?php

// A default path in case "which az" doesn't work.
define('AZ_DEFAULT_PATH', '/usr/bin/az');

// Find the path to az with the "which" command.
$az_path = exec('which az');
if ($az_path === false) {
  $az_path = AZ_DEFAULT_PATH;
}

// Sanitize your variables to avoid shell injection and build the command.
$cmd = $az_path .
       "storage container exists --account-name $accountName " .
       "--account-key $key --name $containeurName";

$last_line = exec($cmd, $full_output, $error_code);

// Then check if $last_line !== false and check $error_code to see
// what happened.
var_export([
  '$cmd' => $cmd,
  '$last_line' => $last_line,
  '$full_output' => $full_output,
  '$error_code' => $error_code,
]);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文