Search code examples
phpajaxshellwake-on-lan

PHP shell command not executed via AJAX-call


In my homeserver management application I added a function to wake PC's remotely via the application. The PHP-script that does this works perfectly, but I want to be able to do this via an AJAX-call.

The call happens like this:

User clicks a PHP-generated link:

<a href=\"javascript:wake('$hostname')\">Wake</a>

Where the function (jQuery) is:

function wake(hostname) {
    $(document).ready(function()
    {
       alert('function works');
       $.post('ajax/wake.php',{host: hostname});
       alert('Command executed');
    });
}

Both alerts are shown, which means the AJAX-call is executed. The php-script looks like this:

<?php
include_once("../classes/BLClient.php");
$blClient = new BLClient(true);
$hostname = $_GET["host"];

$client = $blClient->getClientByHostname($hostname);
$mac = $client->getMac();

echo `sudo etherwake -i eth1 $mac`;
?>

However, my PC's are not woken. If I browse directly to the script, it does work...


Solution

  • You're firing a $.post ajax request, but you're reading a $_GET parameter in your PHP script. Do a $.get ajax request instead, otherwise the data will be in $_POST not in $_GET.

    Alternatively you can make use of the $_REQUEST superglobal which contains both post and get variables in PHP.