sudo in php exec()

后端 未结 7 1137
执笔经年
执笔经年 2020-11-22 09:44

I don\'t know what the deal is here…

So I want to run an applescript: sudo osascript myscript.scpt

This works fine in the terminal, but not when

相关标签:
7条回答
  • 2020-11-22 10:24

    I think you can bring specific access to user and command with visudo something like this:

    nobody ALL = NOPASSWD: /path/to/osascript myscript.scpt
    

    and with php:

    @exec("sudo /path/to/osascript myscript.scpt ");
    

    supposing nobody user is running apache.

    0 讨论(0)
  • 2020-11-22 10:32

    Run sudo visudo command then set -%sudo ALL=(ALL:ALL) to %sudo ALL=(ALL:ALL) NOPASSWD: ALL it will work.

    0 讨论(0)
  • 2020-11-22 10:33

    php: the bash console is created, and it executes 1st script, which call sudo to the second one, see below:

    $dev = $_GET['device'];
    $cmd = '/bin/bash /home/www/start.bash '.$dev;
    echo $cmd;
    shell_exec($cmd);
    
    1. /home/www/start.bash

      #!/bin/bash
      /usr/bin/sudo /home/www/myMount.bash $1
      
    2. myMount.bash:

      #!/bin/bash
      function error_exit
      {
        echo "Wrong parameter" 1>&2
        exit 1
      }
      ..........
      

    oc, you want to run script from root level without root privileges, to do that create and modify the /etc/sudoers.d/mount file:

    www-data ALL=(ALL:ALL) NOPASSWD:/home/www/myMount.bash
    

    dont forget to chmod:

    sudo chmod 0440 /etc/sudoers.d/mount
    
    0 讨论(0)
  • 2020-11-22 10:34

    The best secure method is to use the crontab. ie Save all your commands in a database say, mysql table and create a cronjob to read these mysql entreis and execute via exec() or shell_exec(). Please read this link for more detailed information.

            • killProcess.php
    0 讨论(0)
  • 2020-11-22 10:38

    I had a similar situation trying to exec() a backend command and also getting no tty present and no askpass program specified in the web server error log. Original (bad) code:

    $output = array();
    $return_var = 0;
    exec('sudo my_command', $output, $return_var);
    

    A bash wrapper solved this issue, such as:

    $output = array();
    $return_var = 0;
    exec('sudo bash -c "my_command"', $output, $return_var);
    

    Not sure if this will work in every case. Also, be sure to apply the appropriate quoting/escaping rules on my_command portion.

    0 讨论(0)
  • 2020-11-22 10:44

    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 The shell has a pty (pseudo terminal device, same as you would have in i.e. a ssh session), and you can get the shell as root if desired. Not sure you need root to execute your script, but given you mention sudo it is likely.

    After downloading you would simply use the following code:

    $shell    = \MTS\Factories::getDevices()->getLocalHost()->getShell('bash', true);
    $return1  = $shell->exeCmd('/path/to/osascript myscript.scpt');
    
    0 讨论(0)
提交回复
热议问题