【发布时间】:2012-05-02 10:19:24
【问题描述】:
我正在编写一个使用一些 shell 脚本的 PHP 前端。但是,问题是我必须以特定用户的身份调用非常特定目录(可访问但不属于 www-data)中的脚本。
$command = './myscript.py 2>&1 | tee /tmp/outputfile.txt'; // set the command
$cwd = 'cd /home/secondUser/myscript'; // set current working directory
$wrapper = 'echo "Password" | sudo -u secondUser bash -c'; // add extra wrappers
$fullCommand = $cwd.';'.$wrapper.' "'.$command.'"'; // combine subcommands
$output = system($fullCommand);
我正在尝试不同的执行命令(shell_exec、exec...)。但是,我一直在日志中发现以下错误:
sudo: no tty present and no askpass program specified
问题不仅在于运行 shell 脚本(例如通过编辑 sudoers),还在于将其作为特定的 ruser 运行。
有任何提示或技巧吗?
【问题讨论】:
-
您为什么不直接将信息放入特定目录并通过 Cron Job 收集它,然后使用该信息从 cron 执行您的特定脚本?想象一下有人可以从网站直接 sudo 进入你的机器,这对我来说听起来不安全......