I have an always running Raspberry Pi with LAMP installed, and a second server with more horsepower. I want to be able to start up and shutdown the second server from a PHP page on the Raspberry Pi. My code on the Raspberry Pi page is:
<form action="" method="POST">
<input type="Submit" value="Wake Up" name="WOL_Submit">
<input type="Submit" value="Shutdown" name="WOL_shutdown">
</form>
<?php
if(isset($_POST['WOL_Submit']))
{
exec('sudo etherwake AA:BB:CC:DD:EE:FF');
echo "WOL Packet sent...";
}
if(isset($_POST['WOL_shutdown']))
{
exec('ssh user@192.168.1.100 "sudo /sbin/shutdown -P 0"');
echo "Shutdown sent...";
}
?>
I have set up SSH keys, so I dont need to enter a password when SSH'ing from the Pi to the second server, and this works fine if I SSH normally, using putty for instance.
On the second server I have also added the user to visudo with user ALL=(ALL) NOPASSWD: /sbin/shutdown
.
The Wake on LAN command from the webpage works fine and the second server boots up, but the shutdown command does not. I get the message 'Shutdown sent...' but the server does not shutdown. I have tried swapping the " and ' in the exec('ssh user@192...
command, and also tried using ssh -t
, but the server does not shutdown still.