简体   繁体   English

如何使用 PHP 和密码运行 Bash 脚本

[英]How to run Bash script with PHP and a Password

How can I protect a bash script (Ubuntu) with a password and the call it with PHP?如何使用密码保护 bash 脚本(Ubuntu)并使用 PHP 调用它?

If I simply use ALL=(ALL) NOPASSWD:/var/www/my_bash_script.sh this will allow to PHP to call the script from anywhere anytime without password, that's not what I want.如果我简单地使用ALL=(ALL) NOPASSWD:/var/www/my_bash_script.sh这将允许 PHP 随时随地调用脚本而无需密码,这不是我想要的。

PHP has a number of options to do this. PHP 有许多选项可以做到这一点。

With the very little information given, this sample is just a guess using the shell_exec option.由于给出的信息很少,这个示例只是使用shell_exec选项的猜测。

<?php
header('content-type: text/plain');
echo shell_exec('/var/www/my_bash_script.sh yourPassWord');

The file permissions of the my_bash_script.sh file are important. my_bash_script.sh文件的文件权限很重要。

They need to be suitable for execution by the same user or group that the webserver (effectively the php process that executes the php script and therefore the bash script) uses.它们需要适合由 Web 服务器(实际上是执行 php 脚本和 bash 脚本的 php 进程)使用的同一usergroup执行。

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

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