Question

I'm trying to execute a command as su from php.

When I put the command in terminal or with php /var/www/script.php it works perfect, but when I open script.php through browser it returns - sudo: no tty present and no askpass program specified

Thanks.

Was it helpful?

Solution

According to this:

ssh does not allocate a tty by default when running a remote command. Without a tty, sudo cannot disable echo when prompting for a password. You can use ssh's "-t" option to force it to allocate a tty. Alternately, if you do not mind your password being echoed to the screen, you can use the "visiblepw" sudoers option to allow this.

OTHER TIPS

I recently published a project that allows PHP to obtain and interact with a real Bash shell. Get it here: https://github.com/merlinthemagic/MTS

You can get a real bash shell as root. You can then either trigger your php script or execute the commands directly in bash.

After downloading you would simply use the following code:

    $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');
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top