简体   繁体   English

在特定目录中以其他用户的身份从PHP调用Shell脚本

[英]Calling shell script as another user from PHP in specific directory

I'm writing a PHP front-end that uses some shell scripts. 我正在编写一个使用一些Shell脚本的PHP前端。 However, the problem is I have to call the script in a very specific directory (accessible but not owned by www-data) as a specific user. 但是,问题是我必须以特定用户的身份在非常特定的目录(可访问但非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);

I was trying different execute command (shell_exec, exec...). 我正在尝试其他执行命令(shell_exec,exec ...)。 However, all the time I find following error in logs: 但是,我一直在日志中发现以下错误:

sudo: no tty present and no askpass program specified

The problem is not only to run the shell script (eg by editing sudoers), but to run it as a specific ruser. 问题不仅在于运行shell脚本(例如,通过编辑sudoers),还在于将其作为特定的ruser运行。

Any tips or tricks to do so? 有任何提示或技巧吗?

1) ## Heading ##This is not PHP related, but unix related, so it should probably be moved to some other stackexchange server. 1)##标题##这与PHP不相关,但与unix相关,因此应该将其移至其他stackexchange服务器。

Also the log line you are mentioning is telling you exactly what the problem is. 另外,您提到的日志行还告诉您确切的问题所在。 Sudo will not allow you to pass it a password from a script, it requires a terminal (tty) so that it can turn off local echo (so you don't the the letters you type as a password). Sudo不允许您从脚本中为它传递密码,它需要一个终端(tty),以便它可以关闭本地回显(因此您不必输入要作为密码的字母)。 You can override it in sudoers file. 您可以在sudoers文件中覆盖它。 But you should probably solve it completely differently, either by using private password-less keys to login as different user using SSH, or by allowing to "sudo" a command without password. 但是您可能应该以完全不同的方式解决此问题,方法是使用无私钥的私钥使用SSH以不同用户身份登录,或者通过允许“ sudo”不带密码的命令。 Both are mentioned here: 两者都在这里提到:

2) They already solved this problem at: https://serverfault.com/questions/160524/using-su-inside-of-a-shell-script 2)他们已经在以下位置解决了这个问题: https : //serverfault.com/questions/160524/using-su-inside-of-a-shell-script

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

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