我正在尝试以 su 的形式从 php 执行命令。
当我将命令放入终端或使用它时,php /var/www/script.php
它可以完美运行,但是当我通过浏览器打开 script.php 时,它会返回 -sudo: no tty present and no askpass program specified
谢谢。
据此:_
ssh 在运行远程命令时默认不分配 tty。如果没有 tty,sudo 在提示输入密码时无法禁用回显。您可以使用 ssh 的“-t”选项强制它分配一个 tty。或者,如果您不介意将密码回显到屏幕上,则可以使用“visiblepw”sudoers 选项来允许这样做。
我最近发布了一个项目,它允许 PHP 获取真实的 Bash shell 并与之交互。在这里获取:https ://github.com/merlinthemagic/MTS
您可以以 root 身份获得真正的 bash shell。然后,您可以触发您的 php 脚本或直接在 bash 中执行命令。
下载后,您只需使用以下代码:
$shell = \MTS\Factories::getDevices()->getLocalHost()->getShell('bash', true);
$return1 = $shell->exeCmd('yourFirstCommand');
$return2 = $shell->exeCmd('yourSecondCommand');
//the return will be a string containing the return of the script
echo $return1;
echo $return2;
//or if you want to trigger your script as root
$return3 = $shell->exeCmd('php /my/script.php');
echo $return3;
//If your script contains code to make an SSH connection to another server
//there is a much easier way to do that using the project:
$shell = \MTS\Factories::getDevices()->getRemoteHost('ip_address')->getShellBySsh('username', 'secretPassword');