3

im trying to execute a bash script with a php/html button to wake my nas.

<form action="" method="POST">
    <input type="submit" value="Wake NAS" name="zero" />
</form>
<?php
  if (isset($_POST["zero"])){
    #echo "Hello World!";
    shell_exec("/var/www/html/wakenas.sh &");
  }?>

"Hello World" is printed when button is pressed. but code won't be executed. the wakenas.sh looks like this and works if i execute it over shell

#!/bin/bash
etherwake -D "BC:5F:F4:09:E1:07"
echo "why!?!?!" > "/var/www/html/works.txt"
exit 1

wakenas.sh has all rights

Maybe you guys know why it wont be executed.

thanks in advance

7
  • How do you execute it over shell? shell wakenas.sh? Can you put the outputof ls -l /path/to/wakenas.sh Commented Jun 4, 2015 at 11:00
  • with sh /path/to/wakenas.sh ls -l wakenas.sh: -rwxrwxrwx 1 www-data www-data 126 Jun 4 12:47 wakenas.sh Commented Jun 4, 2015 at 11:07
  • You script seems correct to me, try to var_dump(shell_exec("/var/www/html/wakenas.sh &")); to see whether it returns NULL (php.net/manual/en/…) and check you /var/www/html/works.txt file content Commented Jun 4, 2015 at 11:18
  • 1
    One last thing though, ls -l /var/www/html/works.txt Commented Jun 4, 2015 at 12:46
  • 1
    I'm guessing based on your use case, you're probably accessing the php via a web server like apache? Does the apache user (usually nobody, www-data, or apache2) have rights to execute wakenas.sh? Please be aware that if there is any public access to the page, you should NOT give shell access/execute rights to your web server. Commented Jun 4, 2015 at 18:58

3 Answers 3

1

The easy and secure way of executing your script is to put in sudoers. Assuming your Linux distribution is Debian base and user of who run the web server is www-data, then you can create a file e.g /etc/sudoers.d/wakeup_ether

Cmnd_Alias WAKE_UP_CMD = /var/www/html/wakenas.sh
www-data ALL=NOPASSWD: WAKE_UP_CMD

Modify your script to prefix the command with sudo.

shell_exec("sudo /var/www/html/wakenas.sh &");

Reference: https://help.ubuntu.com/community/RootSudo

Sign up to request clarification or add additional context in comments.

Comments

0

From your dump:

etherwake: This program must be run as root.

when you execute wakenas.sh you probably are executing it as root. That's why it works.

Give the sudo permission (without password) to the user that your php server is running.

And change the wakenas.sh to:

#!/bin/bash
sudo etherwake -D "BC:5F:F4:09:E1:07"
echo "why!?!?!" > "/var/www/html/works.txt"
exit 1

Comments

0

I recently published a project that allows PHP to obtain and interact with a real Bash shell (as root if requested), it solves the limitations of exec() and shell_exec(). Get it here: https://github.com/merlinthemagic/MTS

After downloading you would simply use the following code:

$shell    = \MTS\Factories::getDevices()->getLocalHost()->getShell('bash', true);
$return1  = $shell->exeCmd("etherwake -D \"BC:5F:F4:09:E1:07\"");
//the return will be a string containing the return of the command
echo $return1;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.