I am trying to get the output of a command. For now I'm just trying a simple "ls /home/."
$output = shell_exec("sudo -u myusername ssh remote_server ls /home/");
echo json_encode($output);
This is being called from an Ajax call, and when I log the data returned, I get "null." In /var/log/apache2/error.log, I'm getting:
sudo: no tty present and no askpass program specified
Since the php script is going to run as www-data, I have this in my sudoers file:
www-data ALL= NOPASSWD:/var/www/path/to/the/file.php
What am I doing wrong? I just need to return data from a command on a remote server as www-data.
I tried appending '2>&1' to the command as this answer suggested, but that did not work. However, it did change my console output from
null
to
sudo: no tty present and no askpass program specified