Search code examples
phplinuxshellsudo

PHP sudo: a terminal is required to read the password


I have a script which I want to call from my PHP code. The following is the script file I have:

listwifi.sh

sudo iwlist wlp1s0 scan | grep SSID

To this I've also given executable permission by using sudo chmod +x listwifi.sh

The following I've added at the end pf my sudoers file (using visudo):

apache ALL = NOPASSWD: /var/www/html/mypath/osscripts/listwifi.sh *
www-data ALL = NOPASSWD: /var/www/html/mypath/osscripts/listwifi.sh *

I had also tried with :

apache ALL = NOPASSWD: /usr/bin /var/www/html/mypath/osscripts/listwifi.sh *
www-data ALL = NOPASSWD: /usr/bin /var/www/html/mypath/osscripts/listwifi.sh *

I'm using the following PHP code to call this file:

exec(getenv('BASE_DIR') . "/osscripts/listwifi.sh", $output);
var_dump($output);

But I keep getting the following error:

sudo: a terminal is required to read the password; either use the -S option to read from standard input or configure an askpass helper

How can I make PHP execute this file as root without having to enter the password?


Solution

  • Take the sudo out of the script and call the script as

    sudo /var/www/html/mypath/osscripts/listwifi.sh
    

    The sudoers configuration is for the command being invoked via sudo, not where it's invoked from...