2012-01-14 250 views
-1

我试图从php执行一个命令su。PHP - 以root身份执行命令

当我把命令终端或php /var/www/script.php它可以完美运行,但是当我通过浏览器打开的script.php,则返回 - sudo: no tty present and no askpass program specified

感谢。

+0

那么,什么是问题?哪部分错误信息不清楚? – Jon 2012-01-14 15:24:55

+0

问题是我如何以root身份正确执行命令:) – user997379 2012-01-14 15:29:37

回答

0

根据this:运行远程命令时

SSH不分配默认一个tty。 没有tty,sudo在提示输入密码时无法禁用回显。 你可以使用ssh的“-t”选项强制它分配一个tty。 或者,如果您不介意将密码回显至 屏幕,则可以使用“visiblepw”sudoers选项来允许此操作。

0

我最近发布了一个项目,该项目允许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'); 
相关问题